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.
alex
466k197 gold badges865 silver badges975 bronze badges
asked Oct 6, 2011 at 2:56
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:
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
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
0