Running PHP on the Command Line


Besides connecting PHP to a web server so that the web server can read your scripts and run them, you can also run PHP on the command line. This can be great for testing things out before you go through the whole rigamarole of uploading files to an ISP.

PHP is an interpreted language, which means that when you run it, the PHP interpreter reads your script and interprets each statement, converting it into code your computer can run. In PHP 5.0, the program that actually does the interpretation on the command line is called the Command Line Interpreter (CLI) and is named php. The program that runs with a web server is called php-cgi.

You can run PHP on the command line with the command php. You've got to make sure that the computer can find php, which means either making sure that php (in the PHP bin directory) is in your computer's path or giving the full path to php when you run it. For example, say you had this script, echo.php:

 <?php     echo "Hello from PHP."; ?> 

If php is in your path, you can run this from the command line like so (where % is a generic, cross-platform command-line prompt, and this example assumes you're in the same directory as echo.php):

 %php echo.php 

If this works, you'll see:

 Hello from PHP. 

If it doesn't work, you can specify the exact location of php, which might look like this in Unix or Linux:

 $/usr/local/bin/php echo.php 

And something like this in Windows:

 C:\>C:\php\php echo.php 

The CLI has many command-line options, which you can use to customize its operation. In fact, php can tell you all about its options if you enter php -h to get the full list:

 %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] -- [args...]   -a                 Run interactively   -c <path>|<file>   Look for php.ini file in this directory   -n                 No php.ini file will be used   -d foo[=bar]       Define INI entry foo with value 'bar'   -e                 Generate extended information for debugger/profiler   -f <file>          Parse <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                 Display colour syntax highlighted source.   -v                 Version number   -w                 Display source with stripped comments and whitespace.   -z <file>          Load Zend extension <file>. 

For example, if you want to get a simple text version of the information the phpinfo function prints out, use the -i option this way: %php -i. And you already know you can get the version of PHP with the -v option (note the (cli) in the output, indicating that we're using the CLI):

 %php -v PHP 5.0.0 (cli) (built: Jul 13 2004 21:39:58) Copyright (c) 1997-2004 The PHP Group Zend Engine v2.0.0, Copyright (c) 1998-2004 Zend Technologies 

In Linux and Unix, you can run PHP scripts simply by typing the name of the script on the command line if you indicate where to find PHP with a line that begins with #! (and give the script execution permission):

 #! /usr/bin/php <?php     echo "Hello from PHP."; ?> 



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net