83

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.

Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
asked Jun 2, 2010 at 1:56
6
  • 2
    Which OS are you running this on? Commented Jun 2, 2010 at 2:01
  • @Matthew J Morrison, is this your server or a hosted server? Commented Jun 2, 2010 at 2:20
  • @Inkspeak - it is a hosted server (because I'm cheap) Commented Jun 2, 2010 at 2:31
  • 4
    @Matthew J Morrison, I suspect, as the other answer and comments suggest, that your PHP was compiled sans-CLI support. Your hosting provider may have done that for security concerns. Commented Jun 2, 2010 at 2:54
  • 1
    @Inkspeak that does appear to be the case. Thanks! Commented Jun 2, 2010 at 2:59

6 Answers 6

118
Answer recommended by PHP Collective

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
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Jun 2, 2010 at 2:06
Sign up to request clarification or add additional context in comments.

7 Comments

can you elaborate on what the (cli) really means? I'm pretty sure that is my problem. My --version output for both versions both say (cgi).
@Matthew J Morrison It's the SAPI that's built when you compile PHP with the --enable-cli option.
does that mean that if my installation of PHP was not combiled with the --enable-cli option that there is not a post-compile setting to enable cli?
@Matthew J Morrison It's a separate binary. Maybe it's in your system under a different name/location, maybe not.
Link is dead now.
|
20

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

answered Dec 26, 2013 at 10:31

Comments

6

An extra semicolon is not required at the end.

You can mention php -r "echo 'hi';" instead of php -r "echo 'hi';";

Another example (to get the current timestamp at the command line):

php -r 'print time()."\n";'
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Dec 20, 2013 at 21:47

1 Comment

PS D:\video\TRANSLATE> php -r 'print time()."\n";' PHP Fatal error: Uncaught Error: Undefined constant "n" in Command line code:1 Stack trace: #0 {main} thrown in Command line code on line 1 Fatal error: Uncaught Error: Undefined constant "n" in Command line code:1 Stack trace: #0 {main} thrown in Command line code on line 1
6

The easiest way to do it is to use the -r flag. However, I've found that it does not allow multiline inputs. To work around that, you can do this:

php -r "passthru(file_get_contents('php://stdin'));"

Which lets you pipe from standard input, like so:

echo -n "echo 'test';" | php -r "passthru(file_get_contents('php://stdin'));"

However, to answer the original question, if you do not have the -r flag available, it is also possible to use the -f flag - just pass standard input as the file to open: php -f /dev/stdin

If you do this, be aware that a) you need to include a blank space at the start of the input, and b) you have to open with <?php. Example:

echo -ne " <?php\necho 'test';" | php -f /dev/stdin
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answered Sep 30, 2015 at 9:27

Comments

3

Take a look at this page on PHP command line features, if you haven't already. There are some posts on issues based on OS and double or single quotes.

I would also check the PHP information

php -i

to see if PHP was compiled with CLI support disabled (--disable-cli).

answered Jun 2, 2010 at 2:02

1 Comment

The second link is broken (404).
0

I recently found psysh, which is an interactive debugger also available from the command line.

answered Mar 18, 2022 at 12:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.