Running PHP Script From The Command Line

To run php interactively:

(So you can paste/write code in the console.)

php -a

To make it parse a file and output to the console:

php -f file.php

Parse a file and output to another file:

php -f file.php > results.html

Do you need something else?

To run only a small part, one line or like, you can use:

php -r '$x = "Hello World"; echo "$x\n";'

If you are running Linux then do man php at the console.

If you need/want to run PHP through fpm (FastCGI Process Manager), use cli fcgi:

SCRIPT_NAME="file.php" SCRIP_FILENAME="file.php" REQUEST_METHOD="GET" cgi-fcgi -bind -connect "/var/run/php-fpm/php-fpm.sock"

Where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.

Source