7 essential php command-line options

Most of us use PHP from a IDE or using a simple text editor with a browser, rarely dropping down to the command-line for running php programs. But php provides some interesting and quick options you can use to perform various common tasks or to debug some nasty installation problems. Below is a list of some useful options you should be familiar with.

There are two ways to specify options on the command line – Short Option, where you specify a option by a single character and a Long Option, where you use a more readable string to specify the option, such as ‘–php-ini’. In this post we will be using the Short Option.

PHP Command-line options

1 : Using a different php.ini file

The following option can be useful when you need to test various ini configurations, or you need to check if all the modules in a particular ini are loading correctly.

php –c  PATH/to/PHP.INI example.php

2 : Check to see for any syntax errors in a php file.

Need to quickly check for php syntax errors if you do not have access to an IDE, then this option can be quite helpful. Combine it with some shell code and you can easily check for syntax errors for multiple files in a directory.

php –l example.php

You can for example use the windows command line script to scan all the php files in the current directory for syntax errors.

c:\localhost>for %%X in (*.php) do php -l %%X

3 : Display php builtin and loaded modules

php –m

Combine it with php –c to debug any module loading errors for different php.ini files

php –c PATH/to/PHP.INI -m

This came in handy when I was installing php 5.3 and for some reasons the xdebug extension was not loading. With the help of the above command I was able to narrow down the problem to the following reason:

Xdebug requires Zend Engine API version 220090115.
The Zend Engine API version 220090626 which is installed, is newer.
Contact Derick Rethans at http://xdebug.org for a later 
version of Xdebug.

4 : Syntax Highlighting

The following option will display syntax highlighted text using php’s internal mechanism.

php  –s  example.php

You can then write the output to another file using a redirect.

php  –s  example.php  >  syntax.html

5 : Strip the source code of comments and whitespace.

Can be useful if you need to reduce the file size.

php –w example.php

6 : Run php code from the command line

Directly run php code from the command line without using the php start and end tags.

php -r "$foo = 'hello!'; $foo = strtoupper($foo); echo $foo;"

7 : Display information related to a internal function, class or extension

(Available as of PHP 5.1.2)
These options can be quite handy when a PHP manual is not within reach.

php --r[fcei]

Show information about a function:

php --rf usort

Example output for the above:

Function [  function usort ] {

  - Parameters [2] {
    Parameter #0 [  &$arg ]
    Parameter #1 [  $cmp_function ]

Show information about a class:

php --rc EmptyIterator

Example output for the above:

Class [   class EmptyIterator 
          implements Iterator, Traversable ] {
  - Constants [0] {
  - Static properties [0] {
  - Static methods [0] {
  - Properties [0] {
  - Methods [5] {
    Method [  public method rewind ] {
    Method [  public method valid ] {
    Method [  public method key ] {
    Method [  public method current ] {
    Method [  public method next ] {

Show information about a extension:

php  --re json

Example output for the above:

xtension [  extension #10 json version 1.2.1 ] {

  - Functions {
    Function [  function json_encode ] {
    Function [  function json_decode ] {

Show configuration information for a particular extension:

php  --ri tidy

Example output for the above:


Tidy support => enabled
libTidy Release => 15 August 2007
Extension Version => 2.0
($Id: tidy.c,v 2007/09/20 22:25:05 nlopess Exp $)

Directive => Local Value => Master Value
tidy.default_config => no value => no value
tidy.clean_output => no value => no value

10 thoughts to “7 essential php command-line options”

  1. This is a good article; an approachable reminder of the PHP command line switches. I particularly like the new –rf and other switches, using those a lot when I’m offline and got work to do

Leave a Reply

Your email address will not be published.