Some Useful PHP Command Line Options
When you use a tool day in, day out, you take a lot of things for granted. Sometimes there are some really useful things available but because you don’t look, you miss them. That got me thinking about php.exe and what some alternative options are available which might be useful. This short article will highlight some of those and give some examples and outputs of their use, where appropriate. Let’s start with the most obvious.
Help (-h)
The esteemed help page.
> php -h |
Version (-v)
Not sure why your PHP isn’t recognising some new v7 types of code? You need to check the version number.
> php -v |
Modules Loaded (-m)
This shows which modules are loaded with PHP. Very useful if you want to know if XDebug is installed, for example.
> php -m |
Information (-i)
We’ve all done something like this:
<?php |
but did you know you can do it from the command line? Send the output to a file - it’s going to be big!
> php -i > info.txt |
The Interactive Shell (-a)
With this, you can try out code snippets or even use it like a calculator!
> php -a |
Type “quit“
Running Commands on the Command Line (-r)
Similar to the example above, you can run commands on the command line like so:
> php -r "$i = 1 * 1000; var_dump($i);" |
Watch out for the use of single or double quotes, much like when you use PHP normally.
Viewing Code in Coloured Form (-s)
It’s possible to view your code as colourized HTML. Really! Switch the path to your favourite browser with the command below.
> php -s test.php > test.html && "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" test.html |
Removing Whitespace and Comments (-w)
This is a quite useful option which will remove all comments and blank lines from your source code. So, given this small script:
<?php |
Let’s finish with one more.
Setting Defined Constants
This example is straight from the manual and is a good demonstration:
> php -d max_execution_time |
Hopefully that showed you something you haven’t seen before. Of course, check out the command line help or the manual link above for even more examples such as timing routines, checking syntax and starting in-built servers etc.
Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com