Running php code on Terminal window

To enter the php console in Terminal.
Type the 3 commands below in Termial window, it will enter the php console, print the current timestamp and then exit the php console

php -a
echo date("Y-m-d H:i:s");

To run a php file(echodate.php) from command line.

php -f echodate.php

An example echodate.php which appends a new to the file echodate.txt, creates the file if it doesn’t exist yet.

$file = 'echodate.txt';
$date = date('Y-m-d H:i:s').": This is a line from echodate.phpn";
// Append the $date to the file
file_put_contents($file, $date, FILE_APPEND | LOCK_EX);

To run a small php code snippet on the Terminal line.

php -r '$date=date("Y-m-d H:i:s"); echo $date;'

To direct output from php code to another file.

php -r 'phpinfo();' > phpinfo.txt

Search within Codexpedia

Custom Search

Search the entire web

Custom Search