Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP in command line

Using Python I can test my code in the terminal / command line by typing

python 
python> print "hello world"

I would like to do this with PHP too, but when typing:

php
echo "hello world";

it does not work.. Is this possible? what should I do? A quick search on the internet gives a lot of results that call an actual .php file to run. I only want to test a single sentence if possible, without creating files and stuff.

like image 730
hsmit Avatar asked Nov 07 '10 11:11

hsmit


People also ask

How do I run a PHP command?

system() is just like the C version of the function in that it executes the given command and outputs the result. The system() call also tries to automatically flush the web server's output buffer after each line of output if PHP is running as a server module.


2 Answers

Try

php -a

which starts an interactive PHP shell. Be aware that this requires PHP to be built with --with-readline (which is not the case if you're using the bundeled PHP with Mac OS X e.g.).

Alternatively, if you don't require the interactivity of a separate shell, use

php -r 'print_r(get_defined_constants());'

to execute a PHP snippet (this doesn't require the readline support).

like image 152
Stefan Gehrig Avatar answered Sep 20 '22 05:09

Stefan Gehrig


php -r "echo 'hello world';"
like image 23
bcosca Avatar answered Sep 17 '22 05:09

bcosca