PHP CLI Mode

Source: Internet
Author: User
Tags php cli php script

Reprint : http://www.cnblogs.com/zcy_soft/archive/2011/12/10/2283437.html

All PHP distributions, whether compiled from source code or pre-created versions, are provided with a PHP executable by default. This executable can be used to run the command-line PHP program.
To find this executable file on your system, follow the steps below:
Windows: Placed in the main PHP installation directory, the file name is Php.exe or (in the old version of PHP) is Php-cli.exe.
Linux: Saved in the bin/subdirectory of the PHP installation directory.

It is important to note that the CLI mode and CGI mode runtime php.ini are not the same set of configurations and need to be configured separately.
Regardless of the operating system, you need to test it to make sure it works correctly by calling it with the-v parameter:

5.6.  One (CLI) (Built:jul  9:1997 - the PHP groupzend Engine v2. 6.0 1998- Zend Technologies
View Code

Using CLI commands
A simple PHP CLI program, named hello.php

<?  'Hello cli';
View Code

Now, try running the program at a command-line prompt by invoking the CLI executable and providing the script's file name:

shell>/path/php.exe/example/ from the CLI

Use of standard inputs and outputs
The PHP CLI defines three constants to make it easier to interact with the interpreter at the command-line prompt. These constants are shown in the following table

Constant Description
Stdin
Standard input Devices
STDOUT
Standard output devices
STDERR
Standard error Device

Examples of Use:

<?   '= 'Hello $name ! ");

command Line custom variable 1"$argv | $argc"
It is common practice to enter program parameters at the command line to change how they are run. You can also do this to the CLI program.
The PHP CLI has two special variables that are specifically designed to achieve this purpose:
One is $argv variable, which saves the parameters passed to the PHP script as a separate array element through the command line;
The other is $ARGC variable, which holds the number of elements in the $ARGV array.

Examples of Use:

<? Phpprint_r ($ARGV);

Run:

PHP-F. \cli.php name Age Sexarray (4) {  [0]=>string(9)". \cli.php"  [1]=>string(4)"name"  [2]=>string(3)" Age"  [3]=>string(3)"Sex"}

Note that the first argument $argv is always the script's own name.

Note: We can also use console_getopt the Pear class adds more complex command-line arguments to PHP.

command line custom variable 2"Receive parameters using console_getopt "

Note: This variable is only available when REGISTER_ARGC_ARGV is open

Getopt ($option, $longopts)///First $Option receives-H vb second parameter receives--require SSS

<?PHP $shortopts=""; $shortopts.="F:";//Required Value$shortopts. ="V::";//Optional Value$shortopts. ="ABC";//These options do not accept values$longopts =Array ("Required:",//Required Value            "Optional::",//Optional Value            "option",//No Value            "opt",//No Value        ); $options=getopt ($shortopts, $longopts); Var_dump ($options);

Results:

Php.exe \tools\index.php-f"value for f"-v-a--Required Value--optional="Optional Value"--option Willarray (6) {  ["F"]=>string( One)"value for f"  ["v"]=>BOOL(false)  ["a"]=>BOOL(false)  ["Required"]=>string(5)"value"  ["Optional"]=>string( -)"Optional Value"  ["option"]=>BOOL(false)}

command-line variable 3"using CLI parameters"

In addition to using the command line to pass PHP script parameters, you can also pass the PHP CLI parameter to change how it works.

Parameters Description
-A Running run interactively interactively
-C Path reads PHP's. ini file from path
-N Run directly without reading PHP's. ini file
-M List the compiled modules
-I. Display information about PHP builds
-L Check the syntax of the PHP script
-S Display the source code in a colored fashion
-W Display the source code after removing the comment
-H Show Help

You can also interactively use the PHP CLI, which is the input command, to get results immediately.
To get this effect, you just need to invoke the CLI executable with one parameter, just like this:

Shell>/path/to/php-ainteractive mode enabled<? Phpecho mktime (); 1121187283  2+2; 4 exit ();

Alternatively, you can invoke the CLI executable without using the-a parameter and enter the complete script or code snippet directly.

Use <ctrl>-d to end the code snippet and let the CLI execute it. See the example below

shell>/path/to/php<? Phpecho Date ("d-m-y h:i:s", Time ());? ><Ctrl-D>-jul-2005: Wu:

PHP CLI Mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.