Skip to main content

Executing PHP in command line

Submitted by amitsedai on
There are times when one needs to execute PHP in command like, for example trying the date command output or a one line execution. For trivial tasks, executing the same at the command line without the need for writing and executing at a file level is a great tool for time saving. The argument to be used is : -r Example: php -r '
$a = range(0,2);
foreach($a as $b) {
echo "entry: $b\n";
}
'
The output is:
entry: 0
entry: 1
entry: 2

Another example:
php -r '
> echo date("d-m-Y");
> echo "\n";
> '
14-01-2013

Another useful argument is -l . This can be used for simply testing the syntax and not executing it. $ php -l hello-world.php
No syntax errors detected in hello-world.php

-- Source: http://www.netmagazine.com/tutorials/how-use-php-command-line http://php.net/manual/en/features.commandline.php

Technologies