PHP on the command line on Unix-like operating systems

Running php on the command line is fairly straightforward. First you must locate PHP:

$ type -path php

Some /etc/bashrc files contain an alias that allows:

$ which php

So now that you know where it is, you can create a new php file, starting with your (hash|she)bang:

echo 'This is printed to STDOUT'."\n";

Ok, so it’s still just a text file, not something that can.. execute. Make it so:

$ chmod +x file_name

Now you can run your PHP script!

$ ./file_name
This is printed to STDOUT

There you have it, running a PHP script on the command line of your Unix-like operating system. Running PHP in this fashion retains access to all PHP has running on your web server as a dynamic website. Save for HTTP requests, obviously.

Author: Mike on May 7, 2013
Category: PHP, Tutorials

Comments are closed.

Last articles