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.
I was wondering if someone could explain why it is happening, does it matter at all, and if it is possible to fix it.
Best Answer
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 standardPATH
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:or by changing the
PATH
environment variable for in your PHP script context.