Execute PHP code directly on command line

I always wondered if running PHP code directly in the command line could be posible and today, thanks to a tweet from Symfony creator Fabien Potencier, I learned how to do it. You just have to use the -r option of PHP:

$ php -r 'echo "Hello World!" . PHP_EOL;'
Hello World!

You might also like

Execute PHP code as interactive shell
I explained before how to execute PHP code directly on command line, but if you are just doing some test...

Fix “OSX10.9.xctoolchain/usr/bin/cc: No such file or directory”
You may get this error while compiling something on the command line with Mac OS X:/Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.9.xctoolchain/usr/bin/cc:...

Run a command from a Java application dealing properly with stdin, stdout and stderr
If you want to run an external command from a Java application you have to deal properly with the input,...

Using MacPorts php from command line
Some time ago I had installed Apache+MySQL using MacPorts in Mac OS X. It was all OK as long as I used...

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.