Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a string of PHP code on the command line

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]); ?> 

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)?


Because I don't think it was very clear above, the -r option is not available to me. Here is the php -h output for both versions of PHP that I'm running.

PHP 4.4.9

Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]        php <file> [args...]   -a               Run interactively   -C               Do not chdir to the script's directory   -c <path>|<file> Look for php.ini file in this directory   -n               No php.ini file will be used   -d foo[=bar]     Define INI entry foo with value 'bar'   -e               Generate extended information for debugger/profiler   -f <file>        Parse <file>.  Implies `-q'   -h               This help   -i               PHP information   -l               Syntax check only (lint)   -m               Show compiled in modules   -q               Quiet-mode.  Suppress HTTP Header output.   -s               Display colour syntax highlighted source.   -v               Version number   -w               Display source with stripped comments and whitespace.   -z <file>        Load Zend extension <file>. 

PHP 5.2.13

Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]        php <file> [args...]   -a               Run interactively   -C               Do not chdir to the script's directory   -c <path>|<file> Look for php.ini file in this directory   -n               No php.ini file will be used   -d foo[=bar]     Define INI entry foo with value 'bar'   -e               Generate extended information for debugger/profiler   -f <file>        Parse <file>.  Implies `-q'   -h               This help   -i               PHP information   -l               Syntax check only (lint)   -m               Show compiled in modules   -q               Quiet-mode.  Suppress HTTP Header output.   -s               Display colour syntax highlighted source.   -v               Version number   -w               Display source with stripped comments and whitespace.   -z <file>        Load Zend extension <file>. 

There is no -r option. When I try to use the -r option I get:

Error in argument 1, char 2: option not found r

Sorry for the confusion.

like image 808
Matthew J Morrison Avatar asked Jun 02 '10 01:06

Matthew J Morrison


People also ask

Can we use PHP for command line scripts?

There are two variables you can use while writing command line applications with PHP: $argc and $argv. The first is the number of arguments plus one (the name of the script running). The second is an array containing the arguments, starting with the script name as number zero ($argv[0]).

What is a command line PHP?

PHP's Command Line Interface (CLI) allows you to execute PHP scripts when logged in to your server through SSH. ServerPilot installs multiple versions of PHP on your server so there are multiple PHP executables available to run.

How do I run a PHP file?

php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.


2 Answers

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 
like image 158
Artefacto Avatar answered Oct 21 '22 18:10

Artefacto


In new versions of PHP you just type "php -a" and you hop into an interactive mode, where you can experiment with PHP.

like image 26
Damian W. Avatar answered Oct 21 '22 20:10

Damian W.