Don’t Hardcode PHP Executable Path In Shebang

Some time ago I saw a Sean Coates about not using a hardcoded path to the PHP executable. With the volatile nature of Twitter, I decide to write a blog post repeating his advice.

When you are using PHP for CLI scripts you should avoid starting them with a common shebang like “#!/usr/bin/php”. Not every GNU/Linux distribution or installation method will place the PHP executable in the directory you expect from your own system.

/usr/bin/env can be used to execute commands in a configured environment where the path to the PHP executable (most likely) will appear in the $PATH variable. If it’s not there, it most certainly ain’t in /usr/bin/php.

A much better shebang for your PHP script would be: #!/usr/bin/env php

Read more from the Software engineering category
SUBSCRIBE TO OUR UPDATES
Menu