execute a string of PHP code on the command line

Posted by Matthew J Morrison on Stack Overflow See other posts from Stack Overflow or by Matthew J Morrison
Published on 2010-06-02T01:56:52Z Indexed on 2010/06/02 2:03 UTC
Read the original article Hit count: 222

Filed under:
|

I'd like to be able to run a line of PHP code on the command line similar to how the following options work:

:~> perl -e "print 'hi';"
:~> python -c "print 'hi'"
:~> ruby -e "puts 'hi'"

I'd like to be able to do:

:~> php "echo 'hi';"

I've read that there is a -r option that can do what I need for php, however it doesn't appear to be available when I try to use it. I've tried using PHP 5.2.13 and PHP 4.4.9 and neither have an -r option available.

I wrote this script (that I called run_php.php) - which works, but I'm not a huge fan of it just because I feel like there should be a more "correct" way to do it.

#!/usr/bin/php5 -q
<?php echo eval($argv[1]); ?> 

My question is: is there a -r option? If so, why is it not available when I run --help? If there is no -r option, what is the best way to do this (without writing an intermediary script if possible)?

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about command-line