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
Usage: php [options] [-f] <file> [--] [args...]
php [options] -r <code> [--] [args...]
php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
php [options] -S <addr>:<port> [-t docroot] [router]
php [options] -- [args...]
php [options] -a

-a Run as interactive shell
-c <path>|<file> Look for php.ini file in this directory
-n No configuration (ini) files will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse and execute <file>.
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-r <code> Run PHP <code> without using script tags <?..?>
-B <begin_code> Run PHP <begin_code> before processing input lines
-R <code> Run PHP <code> for every input line
-F <file> Parse and execute <file> for every input line
-E <end_code> Run PHP <end_code> after processing all input lines
-H Hide any passed arguments from external tools.
-S <addr>:<port> Run with built-in web server.
-t <docroot> Specify document root <docroot> for built-in web server.
-s Output HTML syntax highlighted source.
-v Version number
-w Output source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.

args... Arguments passed to script. Use -- args when first argument
starts with - or script is read from stdin

--ini Show configuration file names

--rf <name> Show information about function <name>.
--rc <name> Show information about class <name>.
--re <name> Show information about extension <name>.
--rz <name> Show information about Zend extension <name>.
--ri <name> Show configuration for extension <name>.

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
PHP 7.2.5 (cli) (built: Apr 25 2018 02:39:21) ( ZTS MSVC15 (Visual C++ 2017) x86 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans

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
[PHP Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
hash
iconv
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
readline
Reflection
session
SimpleXML
SPL
standard
tokenizer
wddx
xdebug
xml
xmlreader
xmlwriter
zip
zlib

[Zend Modules]
Xdebug

Information (-i)

We’ve all done something like this:

<?php

phpinfo();

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
Interactive shell

php > echo 1+2;
3
php >

Type “quit to exit.

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);"
Command line code:1:
int(1000)

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

// comment line

echo 'Hello world' . PHP_EOL ;

You end up with this:

> php -w test.php
<?php
echo 'Hello world' . PHP_EOL ;

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
-r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(1) "1"

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