PHP CLI Mode Introduction and use Tutorial

Source: Internet
Author: User
Tags php cli stdin

See full name to know, PHP-CLI is the short name of PHP command line interface, that is, the PHP interface, in Windows and Linux are supported PHP-CLI mode, in fact, both, I have written before:

One, window run the PHP program at the command line
Second, Linux uses crontab to achieve timed tasks
Advantages and application of PHP-CLI mode:

    • 1. Fully support multithreading
    • 2. As above, can achieve the scheduled task
    • 3. Developing desktop apps is using PHP-CLI and GTK packages
    • Write shell scripts in PHP 4.linux


In fact, PHP operating environment far more than Apache and CLI, haha, such as Aolserver, Apache, Apache2filter, Apache2handler, Caudium, CGI (until PHP 5.3), cgi-fcgi, CLI, continuity, embed, ISAPI, Litespeed, Milter, Nsapi, phttpd, Pi3web, Roxen , thttpd, Tux, and Webjames.

You can use Php_sapi_name () to detect, do not believe, look at the following code:


    1. <? PHP
    2. /** reprint Please indicate the link: http://www.phpddt.com * *
    3. echo Php_sapi_name();

I run the results on the browser:
I run on the cmd of Windows:
Use standard input and output:

STDIN Standard input device

STDOUT Standard output device

STDERR standard error device

Look at the results of running the following program:


  1. <? PHP
  2. Ask for input
  3. Fwrite(STDOUT, "Enter Your Name:");
  4. Get input
  5. $name = trim(fgets(STDIN));
  6. Write input back
  7. Fwrite(STDOUT, "Hello, $name!") );

Run:
Use the command-line arguments:
The PHP CLI has two special variables for this purpose: one is the $ARGV variable, which saves the arguments passed to the PHP script as a separate array element through the command line, and the $ ARGC variable, which is used to hold the number of elements in the $ARGV array.
You can run:


    1. <? PHP
    2. /**
    3. * @link http://www.phpddt.com
    4. */
    5. Print_r($argv);

Look at the results:
Use the PHP CLI to pass the script parameters in the form:
Please see the parameters:


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




At this point, I have a certain understanding of the PHP cli, if the reader wants to know more, please refer to the official manual: http://php.net/manual/zh/features.commandline.php

Reprint: http://www.phpddt.com/php/php-cli.html Respect others ' labor achievement is respect oneself!

PHP CLI Mode Introduction and use Tutorial

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.