How can I execute PHP code from the command line?
Solution 1
If you're going to do PHP in the command line, I recommend you install phpsh, a decent PHP shell. It's a lot more fun.
Anyway, the php
command offers two switches to execute code from the command line:
-r <code> Run PHP <code> without using script tags <?..?>
-R <code> Run PHP <code> for every input line
You can use php
's -r switch as such:
php -r 'echo function_exists("foo") ? "yes" : "no";'
The above PHP command above should output no
and returns 0
as you can see:
>>> php -r 'echo function_exists("foo") ? "yes" : "no";'
no
>>> echo $? # print the return value of the previous command
0
Another funny switch is php -a:
-a Run as interactive shell
It's sort of lame compared to phpsh, but if you don't want to install the awesome interactive shell for PHP made by Facebook to get tab completion, history, and so on, then use -a as such:
>>> php -a
Interactive shell
php > echo function_exists("foo") ? "yes" : "no";
no
php >
If it doesn't work on your box like on my boxes (tested on Ubuntu and Arch Linux), then probably your PHP setup is fuzzy or broken. If you run this command:
php -i | grep 'API'
You should see:
Server API => Command Line Interface
If you don't, this means that maybe another command will provides the CLI SAPI. Try php-cli; maybe it's a package or a command available in your OS.
If you do see that your php
command uses the CLI (command-line interface) SAPI (Server API), then run php -h | grep code
to find out which crazy switch - as this hasn't changed for year- allows to run code in your version/setup.
Another couple of examples, just to make sure it works on my boxes:
>>> php -r 'echo function_exists("sg_load") ? "yes" : "no";'
no
>>> php -r 'echo function_exists("print_r") ? "yes" : "no";'
yes
Also, note that it is possible that an extension is loaded in the CLI and not in the CGI or Apache SAPI. It is likely that several PHP SAPIs use different php.ini files, e.g., /etc/php/cli/php.ini
vs. /etc/php/cgi/php.ini
vs. /etc/php/apache/php.ini
on a Gentoo Linux box. Find out which ini file is used with php -i | grep ini
.
Solution 2
Using PHP from the command line
Use "
instead of '
on Windows when using the CLI version with -r
:
Correct
php -r "echo 1;"
Incorrect
php -r 'echo 1;'
PHP Parse error: syntax error, unexpected ''echo' (T_ENCAPSED_AND_WHITESPACE), expecting end of file in Command line code on line 1
Don't forget the semicolon to close the line (otherwise, the result is "PHP Parse error: syntax error, unexpected end of file, expecting ';' or ',' in Command line code on line 1").
Solution 3
You can use:
echo '<?php if(function_exists("my_func")) echo "function exists"; ' | php
The short tag "< ?=" can be helpful too:
echo '<?= function_exists("foo") ? "yes" : "no";' | php
echo '<?= 8+7+9 ;' | php
The closing tag "?>" is optional, but don't forget the final ";"!
Solution 4
On the command line:
php -i | grep sourceguardian
If it's there, then you'll get some text. If not, you won't get a thing.
Solution 5
To run a PHP shell through a Windows command:
php -a
It will open the below PHP shell in Windows, where I was testing a simple square_val()
function:
function square_val($num, $pow){
return $num**$pow;
}
The above code was tested.
To test that the above code works, use echo square_val(4, 2);
and then echo square_val(4, 0.5);
.
Related videos on Youtube
Steve
Updated on September 15, 2021Comments
-
Steve over 2 years
I would like to execute a single PHP statement like
if(function_exists("my_func")) echo 'function exists';
directly with the command line without having to use a separate PHP file.How is it possible?
-
Matt Gibson about 12 yearsdoing function_exists() without using any other files containing user defined function isn't going to be much good, except for testing the PHP version, which you can find out in other ways. What function do you want to test for?
-
Steve about 12 yearsI'm looking for testing this function: sg_load()
-
Matt Gibson about 12 yearsSounds like what you really want is to find out if the sourceguardian php extension is enabled?
-
jpic about 12 yearsthen phpinfo() should tell you.
-
-
jpic about 12 yearsMaybe your php setup is broken. I updated my answer including hints to figure it out.
-
jpic about 12 yearsAdded a note about php -a which might be useful to you.
-
trejder over 9 yearsEither I'm blind or this answer has nothing to do with the question. How this answer is supposed to answer question asking "How to run PHP code directly from command line, without saving PHP code into
.php
file?"? -
Matt Gibson over 9 years@trejder For a moment there I wondered if I'd answered the wrong question, but then I looked at the edit history and comments. This answers what the OP appeared to really need: finding out whether a particular PHP extension was loaded. The question has since been rephrased, so it does not fit so well now.
-
Ben Creasy over 8 yearsNote that phpsh is no longer maintained by its author (Facebook) and someone on the Google Groups page said in 2012 that "phpsh isn't really supported or useful any more". You might look into the Boris REPL for an improved php console.
-
miken32 about 5 yearsThis seems to be more trouble than the 7 year old accepted answer. Using
php -r
you don't need to worry about opening PHP tags. -
Admin about 3 yearssimplest way is to
php -a
I have added a picture and written an answer below -
Peter Mortensen over 2 yearsThe single quotes for the "-r" lines would not work on Windows(?).