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
Answer from Artefacto on Stack Overflow
🌐
PHP
php.net › manual › en › features.commandline.usage.php
PHP: Usage - Manual
If not, or if the argument was ... run the above script on Unix, it must be made executable, and called simply as script.php echothis or script.php -h....
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-execute-php-code-using-command-line
How to Execute PHP Code using Command Line? - GeeksforGeeks
This can be done using the PHP Command Line Interface (PHP CLI), which allows you to execute PHP scripts directly from the command line. This method is commonly used for testing, automation, or running background tasks by using the php command followed by the script name or inline code.
Published   July 12, 2025
Top answer
1 of 7
315

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

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.

2 of 7
16

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").

🌐
IONOS
ionos.com › help › hosting › using-php-for-web-projects › executing-php-files-via-the-command-line-php-cli
Executing PHP files via the command line (PHP CLI) - IONOS Help
Execute the command cd in the terminal ... execute the command. ... To execute a PHP file, enter the path to the PHP CLI program followed by the name of the PHP file in the terminal....
🌐
Educative
educative.io › answers › how-to-execute-php-scripts-on-the-command-line
How to execute PHP scripts on the command line
Now that you have PHP installed and running, we can execute our scripts on the command line or terminal. ... Next, head to your terminal or command prompt and change the directory into the folder containing your PHP files. Let’s say something like this: ... Lastly, enter the following command into your cmd or terminal to run the script file named prog1.php: ... The complete code ...
🌐
A Star Trek Timeline
macs.hw.ac.uk › ~hwloidl › docs › PHP › features.commandline.html
Chapter 23. Using PHP from the command line
However, there's another way of using PHP for shell scripting. You can write a script where the first line starts with #!/usr/bin/php. Following this you can place normal PHP code included within the PHP starting and end tags. Once you have set the execution attributes of the file appropriately ...
🌐
DucXinhIT
ducxinh.com › en › techblog › detailed-guide-on-php-command-line-(cli)-and-essential-commands-for-php-developers
Detailed Guide on PHP Command Line (CLI) and Essential Commands for PHP Developers
January 14, 2025 - You can execute PHP scripts directly from the command line: ... Replace script.php with the path to your PHP file. Runs a single line of PHP code without a script file
Find elsewhere
🌐
PHPpot
phppot.com › php › command-line-php
Command Line PHP - PHPpot
Save this file as print_array.php and specify the name with the PHP command-line option -f. ... We are going to execute PHP code in the command prompt.
🌐
Edureka Community
edureka.co › home › community › categories › web development › php › how to execute php code from the command line
How to execute PHP code from the command line | Edureka Community
October 1, 2020 - I would like to execute a single php statement like if(function_exists("my_func")) echo 'function exists ... seperate php file. How is it possible ?
🌐
W3Docs
w3docs.com › php
Execute a string of PHP code on the command line
To execute a string of PHP code on the command line, you can use the "php" command followed by the "-r" option and the string of code in quotes.
🌐
Szabilinux
szabilinux.hu › php › features.commandline.html
25. Fejezet. Using PHP from the command line
You can write a script where the first line starts with #!/usr/bin/php and then following the normal PHP code included within the PHP starting and end tags and set the execution attributes of the file appropriately. This way it can be executed like a normal shell or perl script: Assuming this file is named test in the current directory, we can now do the following: As you see no care has to be taken when passing parameters to your script which start with -. Táblázat 25-3. Command line options
🌐
TecMint
tecmint.com › home › php › how to use and execute php codes in linux command line – part 1
How to Use and Execute PHP Codes in Linux Command Line - Part 1
July 13, 2015 - Here Option ‘-f‘ parse and execute the file that follows the command. 2. We can use phpinfo() which is a very valuable debugging tool directly on the Linux command-line without the need of calling it from a file, simply as: ... Here the option ‘-r‘ run the PHP Code in the Linux Terminal directly without tags < and >.
🌐
ShellHacks
shellhacks.com › home › run php script from command line
Run PHP Script From Command Line - ShellHacks
January 14, 2022 - How to run a PHP script from the command line on Windows, Linux and MacOS and how to install a PHP CLI (PHP Command Line Interface).
🌐
w3tutorials
w3tutorials.net › blog › how-can-i-execute-php-code-from-the-command-line
How to Execute PHP Code from the Command Line: Run Single Statements Without a Separate File — w3tutorials.net
Using a here-document (more readable for multi-line code): php <<<PHP \$greeting = "Hello"; \$name = "Bob"; echo "\$greeting, \$name!\n"; PHP ... Hello, Bob! Note: Escape $ with \ in here-documents to prevent shell variable expansion. PHP CLI output defaults to stdout (your terminal). You can redirect it to files or pipes, just like any shell command.
🌐
Quora
quora.com › How-can-we-execute-PHP-script-using-command-line
How can we execute PHP script using command line? - Quora
Use -r for one-liners (no <?php tags). ... Include <?php at top when using full script. ... Environment variables available via getenv() or $_ENV. ... Use systemd, cron, or at for scheduled/managed execution; cron example: */5 * * * * /usr/bin/php /path/script.php ... If executing web-oriented scripts that depend on HTTP context ($_SERVER, $_SESSION), adapt code ...