Home » PHP » PHP command line scripting development

PHP command line scripting development

What can
PHP do?

PHP official document shameless to say that PHP can do anything , this and other industry widely circulated a programmer at PHP is the best language is Yaohuxiangying.

PHP is mainly used in the following three fields:

(1) server script

, this is the main domain, and PHP parsers (CGI or server modules) and web servers (such as Apache, Nginx) are paired with.

(2) command line script

this way only requires the PHP parser to execute. Think about Python, and.

will understand

(3) desktop application

can use PHP to write desktop applications through some extensions, such as PHP-GTK, but it's too boring to do it,.

Command line development
The following

operations are performed under Mac

enters the php directory, or puts the php directory into the environment variable. (Mac ignores this step)

check the PHP engine

php -v
# output
PHP 5.5.27 (CLI) (built:, Jul, 232015, 00:21:59)
Copyright (c) 1997-2015, The, PHP, Group
Zend, Engine, v2.5.0, Copyright (c) 1998-2015, Zend, Technologies

view using help

php -h
# output
Usage: PHP [options] [-f] [args [--]]...
PHP [options] -r [args [--]]...
PHP [options] [-B ] -R [-E ] [args [--]]...
PHP [options] [-B ] -F [-E ] [args [--]]...
PHP, [options], -S, : , [-t, docroot]
PHP [options] - [args...]
PHP [options] -a
-a, Run, as, interactive, shell
-c, |, 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, , Parse, and, execute, .
-h This help
-i PHP information
-l, Syntax, check, only (lint)
-m, Show, compiled, in, modules
-r , Run, PHP, without, using, script, tags, <.. >
-B, , Run, PHP, , before, processing, input, lines
-R, , Run, PHP, , for, every, input, line
-F, , Parse, and, execute, , for, every, line, input
-E, , Run, PHP, , after, processing, all, lines, input
-H, Hide, any, passed, arguments, from, external, tools.
-S : , Run, with, built-in, web, server.
-t, , Specify, document, root, , for, built-in, server., web
-s, Output, HTML, syntax, highlighted, source.
-v Version number
-w, Output, source, with, stripped, comments, and, whitespace.
-z, , Load, Zend, extension, .
Args... Arguments, passed, to, script., Use - args, when, first, argument
Starts with - or script is read from stdin
--ini, Show, configuration, file, names
--rf, , Show, information, about, function, .
--rc, , Show, information, about, class, .
--re, , Show, information, about, extension, .
--rz, , Show, information, about, Zend, extension, .
--ri, , Show, configuration, for, extension, .

executes a PHP file,

php [-f] xxx.php

can pass arguments

php [-f] xxx.php'hello''world' 2015


passed to the script of the parameters can be obtained in the global variable $argv, the number of global variables of $argc being members of the $argv array variable (rather than the number of transmit to the

script program parameters)


Var_dump ($argc);
Echo "n"";
Var_dump ($argv);

executes 001.php

php 001.php'Hello world'2015


int (3)
Array (3) {
String (7) "001.php""
String (11) Hello world"
String (4) 2015"

can also run PHP code

php -r'echo "Hello Worldn";
# output
Hello World

of course, the development of PHP commands not only these contents, here you can see more "nofollow http://php.net/manual/zh/features.commandline.php

copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted