Execute a string of PHP code on the command line
Yep, it's there in PHP 5.2's cli SAPI.
If you cannot upgrade and that's the case that there's no such option in PHP 5.2 (I don't have it at hand to test), you can do this:
echo "<?php echo \"hi\\n\";" | phphi
Original:
There is indeed a -r
option (though I'm not sure about PHP 5.2):
php -r "echo 'hi';";hi
Just make sure you are using the command line version of PHP. php --version
should give you something like this (notice "cli"):
php --versionPHP 5.3.0 (cli) (built: May 20 2010 19:05:12) (DEBUG)Copyright (c) 1997-2009 The PHP GroupZend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
In new versions of PHP you just type "php -a" and you hop into an interactive mode, where you can experiment with PHP.
An extra semicolon is not required at the end.
You can mention php -r "echo 'hi';"
instead of php -r "echo 'hi';";
Another example (to get the current timestamp at the command line):
php -r 'print time()."\n";'