When I am running command inside a php script
echo shell_exec("which php");
I am getting the following output:
/usr/bin/php
However when running the same command inside mac terminal
which php
i am getting the following output
php: aliased to /Applications/MAMP/bin/php/php5.5.10/bin/php
my question is how to let shell_exec act like if commands are running inside mac terminal?
note: that i have ZSH installed
Short answer:
This will not work (reliably).
Long answer:
The problem is threefold:
/bin/sh
which
depends on$PATH
To see 1. You can print the name of the running shell by echoing
$0
As you can see, PHP starts
sh
instead ofzsh
. That meens it also uses the builtins ofsh
or looks for a command if there is no builtin:Unless
sh
links tozsh
, that meens, if you want to make use of zsh's builtins you have to run your command withzsh
:This starts
/bin/sh
, which in turn startszsh
, which then runs the command.While you can work around PHP using
sh
, the second problem is more grave: Aliases are only set within the instance in which they are defined. Most of the time this happens in some configuration file (e.g.~/.zshrc
). But these configuration files are not loaded when usingzsh
non-interactively and neither are aliases passed to child processes:In conclusion that means, that using
which
from inside a PHP script is an entirely unreliable way to find out the origin/location ofphp
. Even more so as the output of which depends on$PATH
, which may also be different for interactive and non-interactive shells.You can't do that on php level.
The output you get from you call is generated by the shell programm executing your request. That shell is not within php, but a shell controlled on a system and its behavior is also system dependent. You cannot expect the same behavior on your local Linux system as you would get from some Mac system.
If your question is why there are different outputs generated in both cases then the answer is: because different php interpreters are installed. Obviously the shell points you to the one installed and preferred on the system it is running on. Everything else would not make sense. The fact that in the second example php is an alias inside that shell which points to that path is no information of any value on your local Linux system.