Run simple php functions direct on command line

If you want to run/test oneliners in PHP on the command line you can use the -r option.

php -r 'echo "Hello World";' 

Will simply output “Hello World” on the command line

I sometimes use this method to test regexp expressions

php -r 'echo preg_match("/^Test/", "This is a test");'

Here I check if $string starts with the string ‘Test’ (the answer is no).

Using onliners on the command line can sometimes be very handy

Comments are closed.

Niklas Tech Blog
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.