Command-line PHP

ript>

Working with the command-line version of PHP can be very enlightening some times. For example, consider the following situations:

  • Instead of having a test page with only phpinfo() in it, just to view PHP’s current settings, you can issue:

    php -i

  • Suppose you want to check your PHP code quickly for any errors. Executing

    php -l filename.php

    can easily reveal any pitfalls.

  • If you want to employ your code in a production environment, your don’t need any textual redundancies, so you can strip all the comments and whitespace by using

    php -w filename.php > ../production_directory/filename.php

  • Finally, suppose you’ve just added/enabled a new module and you want to know if you did everything correctly. Then

    php -m | grep “modulename”

    will answer this question.

For more information, read the manual page of php!

Of course, to be able to do all the above, you have to install the CLI of PHP. For a system that uses the apt-get family of scripts, this is as easy as running:

sudo apt-get install php5-cli

For other systems, I believe that commands like

yum install php5-cli

or

rpm -Uhv php5-cli

will work the same. Check your system’s documentation about the correct command and syntax.

Being able to run PHP scripts from the shell is a very powerful capability, especially if one doesn’t feel comfortable with shell scripts or other interpreted languages, such as PERL or Python.


Visit The Light of the LAMP blog for more…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.