7 essential php command-line options


Posted in: addins, php, tip | Save to del.icio.us | Twit This! 11 Dec 2009

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 [ <internal:standard> function usort ] {
 
  - Parameters [2] {
    Parameter #0 [ <required> &$arg ]
    Parameter #1 [ <required> $cmp_function ]
  }
}

Show information about a class:

php --rc EmptyIterator

Example output for the above:

Class [ <internal:SPL> <iterateable> class EmptyIterator 
          implements Iterator, Traversable ] {
  - Constants [0] {
  }
  - Static properties [0] {
  }
  - Static methods [0] {
  }
  - Properties [0] {
  }
  - Methods [5] {
    Method [ <internal:SPL, prototype Iterator> public method rewind ] {
    }
    Method [ <internal:SPL, prototype Iterator> public method valid ] {
    }
    Method [ <internal:SPL, prototype Iterator> public method key ] {
    }
    Method [ <internal:SPL, prototype Iterator> public method current ] {
    }
    Method [ <internal:SPL, prototype Iterator> public method next ] {
    }
  }
}

Show information about a extension:

php  --re json

Example output for the above:

xtension [ <persistent> extension #10 json version 1.2.1 ] {
 
  - Functions {
    Function [ <internal:json> function json_encode ] {
    }
    Function [ <internal:json> function json_decode ] {
    }
  }
}

Show configuration information for a particular extension:

php  --ri tidy

Example output for the above:

tidy
 
Tidy support => enabled
libTidy Release => 15 August 2007
Extension Version => 2.0
($Id: tidy.c,v 1.66.2.8.2.24 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



Share this post

Share on Facebook
Share on Twitter
Share on StumbleUpon
Share on Delicious
Share on Digg
Share on Technorati
Share on Reddit
Feeds RSS Subscribe to site Feed

Other related posts

  • No Related Post


9 Responses

1

Sameer Borate’s Blog: 7 essential php command-line options | Webs Developer

December 14th, 2009 at 7:01 am

[...] his Code Diesel Sameer shares seven essential options you can use to make your command-line PHP experience even better. Most of us use PHP from a IDE [...]

2

Caio Moritz Ronchi

December 14th, 2009 at 7:38 am

Very useful tips. I’ll try to remember the “–ri” option from now on.

Thanks

3

Tweets that mention 7 essential php command-line options : CodeDiesel -- Topsy.com

December 14th, 2009 at 8:51 am

[...] This post was mentioned on Twitter by Milan Herda, rmartinez. rmartinez said: Awesomeness, The 7 essential PHP Command-line Options http://ping.fm/XOIwm [...]

4

xrado

December 14th, 2009 at 9:51 am

best one is missing (at least for me)
php -a ….php interactive shell

5

LornaJane

December 15th, 2009 at 1:50 am

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

6

Matthias Willerich

December 15th, 2009 at 8:03 am

php -ri is definitely a good one.

xrado: I’ve found that the interactive shell is quite hard to use, actually, exiting when your code dies, not supporting autoload, etc. I just stumbled across iphp http://github.com/apinstein/iphp, and if it keeps its promises it’s much more convenient.

7

7 options en lignes de commandes | traffic-internet.net

December 15th, 2009 at 11:07 am

[...]  7 essential php command-line options (0 visite) [...]

8

Satya Prakash

December 16th, 2009 at 3:50 am

This command is very useful

9

03/01/2010 : Php day | Dev | My Daily Top RSS Selection

January 3rd, 2010 at 12:12 pm

[...] 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. [...]

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]
  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]
  • Martin: How can you carry over your own variables into the URL? I am using a form to POST a couple of var [...]
  • nancy: thanks very much ! first tools [...]

  • Users Online

    • 11 Users Online
    • 7 Guests, 4 Bots