Using PHP from the command line

Written by

in

Got a love note from someone who didn’t appreciate my most web site critique – they said “seems like you are mostly just critisizing, rather than teaching or training, as your banner suggests” … the carnal me wants to reply with a Bronx cheer … but the Christ in me says just move on and return evil with good by posting something really geeky. Especially for those of you with Linux/Apache like web server configurations.

Next time you ssh into your bash and pico a problematic PHP program, add ‘#!/usr/bin/php‘ to the very to of the script and run it from the command line instead of your browser:

#!/usr/bin/php -q
<?php echo “hello world!”; ?>

The nice thing about this is that you can always redirect your STDOUT into file using a syntax to the effect of:

$ chmod 755 helloworld.php
$ php helloworld.php > hello.html

Of course, like any command line tool, there are plenty of arguments and options for PHP which include:

Usage: php [-q] [-h] [-s [-v] [-i] [-f <file>] | {<file> [args…]}
-q     Quiet-mode. Suppress HTTP Header output.
-s     Display colour syntax highlighted source.
-w     Display source with stripped comments and whitespace.
-f     <file>     Parse <file>.     Implies `-q’
-v     Version number
-C     Do not chdir to the script’s directory
-c     <path>     Look for php.ini file in this directory
-a     Run interactively
-d     foo[=bar]     Define INI entry foo with value ‘bar’
-e     Generate extended information for debugger/profiler
-z     <file>     Load Zend extension <file>.
-l     Syntax check only (lint)
-m     Show compiled in modules
-i     PHP information
-h     This helpv

Okay Kiddies, now it’s time for the bonus round. Those of you who are asking “yo dean, what about STDERR and STDIN …” … for that and more gory details on how to PHP from the command line, here are some links you might find useful:

Now you don’t have any excuses not to test your code !-)

Comments

One response to “Using PHP from the command line”

  1. Comand Line PHP

    Dean has some useful pointers on using PHP from the command line. I’ll be doing this when I return in the New Year, so this might come in handy.