PHP offers to execute script through command line.
To execute PHP script in command line, use php scriptname.php
in command line.
php test.php
To PHP accepts command line arguments and deliver through the variable argv
.
php test.php arg1 arg2 arg3
foreach ($argv as $arg) {
echo $arg . "\n";
}
-
Basic Options:
-a
: Interactive mode. Reads and executes code interactively.-c <path>
: Specifies the path to the php.ini file.-d <foo=bar>
: Defines an INI entry.-e
: Generates extended information for debuggers/profilers.-f <file>
: Specifies the file to execute.-h
: Displays help information.-i
: Prints information about the PHP configuration.-l
: Performs a syntax check only for the script.-m
: Lists loaded modules.-n
: Disables the use ofphp.ini
.-r <code>
: Executes a single line of PHP code.-s
: Outputs a syntax-highlighted source code.-v
: Prints the PHP version.
-
Additional Options:
-B <begin_code>
: Executes code before processing input lines.-E <end_code>
: Executes code after processing all input lines.-F <file>
: Parses and executes a file for each input line.-R <code>
: Executes code for every input line.-S addr:port
: Runs a built-in web server for current directory.-t <docroot>
: Specifies the document root for the built-in web server.
Example:
- Checking the PHP version:
php -v
- Running PHP interactively:
php -a
- Syntax checking a script:
php -l my_script.php
- Checking PHP configuration:
php -i
- Starting a built-in web server:
php -S localhost:8000
- Running PHP code directly:
php -r "echo 'Welcome';"