How can I simply run a PHP script in the command line? Also is it possible to run it like it would be if the URL was typed in?
2 Answers
As long as you have php installed, you run a PHP file using
/usr/bin/php /path/to/php/file.php
Or if your $PATH is set up properly to include /usr/bin, then simply
php /path/to/php/file.php
You can check if PHP is installed, by running
which php
-
1Even better would be to just run $(which php) /path/to/php/file.php. Then if php is installed somewhere else, say /usr/local/bin/php or something, then this will run from that bin location. Nov 29, 2016 at 5:15
Use the php5
command:
php5 /path/to/php/script
It's part of the php5-cli
package.
The php
command is part of the alternatives system, so it always points to something else. Depending on the version of PHP your script is in, it would be better to use the versioned command.
Running a PHP script the way it is called by a webserver is a bit complicated. The server sets up quite a few variables, which may or may not be used by the script. It would be much more simpler to run a webserver itself.