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\";" | php

hi

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 --version

PHP 5.3.0 (cli) (built: May 20 2010 19:05:12) (DEBUG)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)