Table 23-3. Command line options
Option Long Option Description
-s --syntax-highlight Display colour syntax highlighted
source.
This option uses the internal mechanism to parse the file
and produces a HTML highlighted version of it and writes
it to standard output. Note that all it does it to generate
a block of <code> [...] </code> HTML tags, no
HTML headers.
Note: This option does not work together with the -r option.
-s --syntax-highlighting Alias of --syntax-highlight.
-w --strip Display source with stripped comments and whitespace.
Note: This option does not work together with the -r option.
-f --file Parses and executed the given filename to the
-f option. This switch is optional and can be left out.
Only providing the filename to execute is sufficient.
-v --version Writes the PHP, PHP SAPI, and Zend version
to standard output, e.g. $ php -v
PHP 4.3.0 (cli), Copyright (c) 1997-2002 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2002 Zend Technologies
-c --php-ini With this option one can either specify a directory
where to look for php.ini or you can specify a custom INI
file directly (which does not need to be named php.ini),
e.g.: $ php -c /custom/directory/ my_script.php
$ php -c /custom/directory/custom-file.ini my_script.php
-n --no-php-ini Ignore php.ini at all. This switch is available
since PHP 4.3.0.
-d --define This option allows you to set a custom value
for any of the configuration directives allowed in php.ini.
The syntax is: -d configuration_directive[=value]
Examples (lines are wrapped for layout reasons): # Omitting
the value part will set the given configuration directive
to "1"
$ php -d max_execution_time
-r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(1) "1"
# Passing an empty value part will set the configuration
directive to ""
php -d max_execution_time=
-r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(0) ""
# The configuration directive will be set to anything passed
after the '=' character
$ php -d max_execution_time=20
-r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(2) "20"
$ php
-d max_execution_time=doesntmakesense
-r '$foo = ini_get("max_execution_time"); var_dump($foo);'
string(15) "doesntmakesense"
-a --interactive Runs PHP interactively.
-e --profile-info Generate extended information for debugger/profiler.
-z --zend-extension Load Zend extension. If only a filename
is given, PHP tries to load this extension from the current
default library path on your system (usually specified /etc/ld.so.conf
on Linux systems). Passing a filename with an absolute path
information will not use the systems library search path.
A relative filename with a directory information will tell
PHP only to try to load the extension relative to the current
directory.
-l --syntax-check This option provides a convenient way
to only perform a syntax check on the given PHP code. On
success, the text No syntax errors detected in <filename>
is written to standard output and the shell return code
is 0. On failure, the text Errors parsing <filename>
in addition to the internal parser error message is written
to standard output and the shell return code is set to 255.
This option won't find fatal errors (like undefined functions).
Use -f if you would like to test for fatal errors too.
Note: This option does not work together with the -r option.
-m --modules Using this option, PHP prints out the built
in (and loaded) PHP and Zend modules: $ php -m
[PHP Modules]
xml
tokenizer
standard
session
posix
pcre
overload
mysql
mbstring
ctype
[Zend Modules]
-i --info This command line option calls phpinfo(), and
prints out the results. If PHP is not working correctly,
it is advisable to use php -i and see whether any error
messages are printed out before or in place of the information
tables. Beware that the output is in HTML and therefore
quite huge.
-r --run This option allows execution of PHP right from
within the command line. The PHP start and end tags (<?php
and ?>) are not needed and will cause a parser error
if present.
Note: Care has to be taken when using this form of PHP
to not collide with command line variable substitution done
by the shell.
Example showing a parser error $ php -r "$foo = get_defined_constants();"
Command line code(1) : Parse error - parse error, unexpected
'='
The problem here is that the sh/bash performs variable substitution
even when using double quotes ". Since the variable
$foo is unlikely to be defined, it expands to nothing which
results in the code passed to PHP for execution actually
reading: $ php -r " = get_defined_constants();"
The correct way would be to use single quotes '. Variables
in single-quoted strings are not expanded by sh/bash. $
php -r '$foo = get_defined_constants(); var_dump($foo);'
array(370) {
["E_ERROR"]=>
int(1)
["E_WARNING"]=>
int(2)
["E_PARSE"]=>
int(4)
["E_NOTICE"]=>
int(8)
["E_CORE_ERROR"]=>
[...]
If you are using a shell different from sh/bash, you might
experience further issues. Feel free to open a bug report
or send a mail to phpdoc@lists.php.net. One can still easily
run into troubles when trying to get shell variables into
the code or using backslashes for escaping. You've been
warned.
Note: -r is available in the CLI SAPI and not in the CGI
SAPI.
-h --help With this option, you can get information about
the actual list of command line options and some one line
descriptions about what they do.
-? --usage Alias of --help.
The PHP executable can be used to run PHP scripts absolutely
independent from the web server. If you are on a Unix system,
you should add a special first line to your PHP script,
and make it executable, so the system will know, what program
should run the script. On a Windows platform you can associate
php.exe with the double click option of the .php files,
or you can make a batch file to run the script through PHP.
The first line added to the script to work on Unix won't
hurt on Windows, so you can write cross platform programs
this way. A simple example of writing a command line PHP
program can be found below.