36

Say I have a block of code I would like to test like this:

<?php 
 Print "Hello, World!";
?>

How I quickly run this code from terminal without saving it to a file?

I tried things like...

php -r "Print "Hello, World!";"

but just got complaints about syntax errors. There has to be a simple way of doing this. I just have yet to find any explanations.

0

2 Answers 2

75

To run PHP commands immediately on Terminal you can pass -a option to your installed PHP:

php -a

Details:

enter image description here

php -a opens an interactive shell which let you type directly PHP commands and view the result on your terminal, As an example, after typing php -a in terminal you can type echo 'Hello World'; and after Press Enter Hello World! will be printed on the screen.

Windows Solution

On windows there is no interactive mode same as Linux but still you can use interactive like mode!, So, open PHP on place you installed it for example if you use XAMPP then your PHP should be is on C:\xampp\php (or add the binary directory to environment variables) and then type php -a, At the end of each line you can view the results by pressing Ctrl+Z and then Enter.

php -a
echo 'hello world!';
^Z
3
  • 1
    It is better to use the lowercase form: php -a so that you can run the command in all operating systems.
    – Farahmand
    Feb 22, 2014 at 15:18
  • this could be a much better answer if it explained what PHP -a actually does
    – Damon
    Mar 10, 2014 at 20:02
  • @Damon tnx Damon, i added some details. Mar 12, 2014 at 10:57
49

Escape the inside double quotes (") that you are using to delimit your string.

php -r "Print \"Hello, World!\";"

Alternatively, use single quotes (') for the PHP string or for the quoting of the PHP code.

If you run php --help you can see a list of commands that the php program accepts.

  -a               Run as interactive shell
  -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 and execute <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
  -R <code>        Run PHP <code> for every input line
  -F <file>        Parse and execute <file> for every input line
  -E <end_code>    Run PHP <end_code> after processing all input lines
  -H               Hide any passed arguments from external tools.
  -S <addr>:<port> Run with built-in web server.
  -t <docroot>     Specify document root <docroot> for built-in web server.
  -s               Output HTML syntax highlighted source.
  -v               Version number
  -w               Output source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin

  --ini            Show configuration file names

  --rf <name>      Show information about function <name>.
  --rc <name>      Show information about class <name>.
  --re <name>      Show information about extension <name>.
  --rz <name>      Show information about Zend extension <name>.
  --ri <name>      Show configuration for extension <name>.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.