How do i run a php command in terminal?

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


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.

How do i run a php command in terminal?

alex

466k197 gold badges865 silver badges975 bronze badges

asked Oct 6, 2011 at 2:56

How do i run a php command in terminal?

Alex EftimiadesAlex Eftimiades

2,4473 gold badges24 silver badges33 bronze badges

0

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

php -a

Details:

How do i run a php command in terminal?

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

answered Nov 9, 2012 at 0:28

How do i run a php command in terminal?

Mehdi HosseiniMehdi Hosseini

1,6671 gold badge17 silver badges29 bronze badges

3

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

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

  --ini            Show configuration file names

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

answered Oct 6, 2011 at 2:58

How do i run a php command in terminal?

0

How do I run a PHP script?

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.

How do I run a PHP script in Linux?

You can execute linux commands within a php script - all you have to do is put the command line in brackits (`). And also concentrate on exec() , this and shell_exec() ..

Is PHP used for command line?

As of version 4.3. 0, PHP supports a new SAPI type (Server Application Programming Interface) named CLI which means Command Line Interface. As the name implies, this SAPI type main focus is on developing shell (or desktop as well) applications with PHP.

How do I run a PHP file on Mac?

Use the Built-In Apache Web Server to Run PHP on Mac We can use the command sudo apachectl start in the terminal to start the webserver. Then, typing the URL http://localhost/index.php where our PHP file is index. html will run the PHP file. The PHP file should be in the root directory to run.