0

I have upgraded PHP version on my Mac to 7.3 with a command

curl -s http://php-osx.liip.ch/install.sh | bash -s 7.3

and then

export PATH=/usr/local/php5/bin:$PATH

When I type php -v in terminal the new version shows up correctly.


I am using Apache shipped with macOS.

I have created a simple document in localhost/phpinfo.php that contains:

echo exec('which php');

echo '<br>';

$out = [];
exec("php -v" , $out);
foreach($out as $line) {
    echo $line, '<br>';
}

phpinfo();

The phpinfo() shows the new PHP 7.3.8 as expected, but the preceding lines, using exec() command produce a different result.

enter image description here

I was wondering if someone could explain why it is happening, does it matter at all, and if it is possible to fix it.

  • Not sure what you mean "in a browser?" What web server/SAPI are you using? – RitterKnight Jun 22 at 15:09
  • The comment re using.bash_profile is wrong. However the correct answer depends on making it clearer how you run the app and how you want to run the app headless – mmmmmm Jun 22 at 17:33
  • I have updated the question, hopefully it is more clear. – Maciek Rek Jun 22 at 19:47
1

This happens because you've still got the older PHP binary installed (probably in /usr/bin).

When you change your PATH environment variable on the command to include /usr/local/bin - this means that the shell also searches that folder (and in that order) to find the program you want to run. In this case when you run php, it finds the new version in /usr/local/bin and runs that for you.

However that PATH environment variable change is local to that shell session only. It is not saved anywhere, and not used in other contexts.

Thus when you run the phpinfo.php script through the web server, it runs with a standard PATH that doesn't include /usr/local/bin. Thus the older PHP binary is run - showing the old version number.

It really doesn't matter at all - why would you want to run a PHP binary from a PHP script run by the webserver anyways?

In case you really wanted to do that and wanted it "fixed", you could do that by either changing your exec() call to include the full path:

exec("/usr/local/bin/php -v" , $out);

or by changing the PATH environment variable for in your PHP script context.

| improve this answer | |
  • I think that some web applications might make use of exec() and the results could be unpredictable/harder to debug if PHP versions are different, thus my question. I see your point that it doesn’t really make sense to 'run a PHP binary from a PHP script’. Thanks for your comprehensive answer. – Maciek Rek Jun 22 at 21:45
  • Hmm, that would be some odd web applications I would say... that's definitely not high on the "recommended list" for developers... in practice I don't think you'll have any problems. – jksoegaard Jun 22 at 22:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .