PHP CLI Mode

Source: Internet
Author: User
Tags php cli sapi


PHP command-line mode

Starting with version 4.3.0, PHP provides a new type of CLI SAPI (Server application Programming Interface, service-side application programming port) support, called CLI, which means Command line Interface , which is the command-line interface. As the name implies, the CLI SAPI module is primarily used as a development shell application for PHP. There are many differences between the CLI SAPI and other CLI SAPI modules, which we will elaborate in this chapter. It is worth mentioning that the CLI and CGI are different SAPI, although there are many common behaviors between them.

Advantages and application of PHP-CLI mode

    1. Full support for multithreading

    2. Implementing timed Tasks

    3. Developing desktop apps is using PHP-CLI and GTK packages

    4. Write shell scripts in PHP under Linux

PHP command-line mode extension

In fact, PHP operating environment far more than Apache and CLI, 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. can use Php_sapi_name () this letter Number to detect, here only the Apache server and the Windows cmd extension, write a cli.php file to test:

<? PHP Echo "PHP Current CLI mode:". Php_sapi_name ();

Windows cmd command-line mode run results:

Running results in Apache server mode:

PHP command-line arguments

Like all shell applications, PHP binaries (php.exe files) and their PHP scripts can accept a range of parameters. PHP does not limit the number of parameters that are passed to the script (the shell has a limit on the number of characters on the command line, but it usually does not exceed the limit). the parameters passed to the script can be obtained in the global variable $argv . The name of the script whose members are labeled zero in the array is "-" when the PHP code comes from standard input and is run directly with the-R parameter on the command line. In addition, the global variable $ARGC the number of member variables in the $ARGV array (not the number of parameters passed to the script).

The PHP CLI has two special variables that are specifically designed to do this: one is $argv variable, which saves the parameters passed to the PHP script as a separate array element through the command line, and the other is $ARGC variable, which is used to save $ The number of elements in the argv array.

Create a test file cli.php:

<? PHP Echo "ARGV:". Print_r ($argv). " \ r \ n "; Echo "ARGC:". $ARGC;

The test results are as follows:

For more information, please refer to the official manual: http://php.net/manual/zh/features.commandline.php

PHP exec () function no return value troubleshooting method

EXEC executes a command at the command line without problems, but in PHP there is an error. This issue 99.99% is related to permissions, but the command that exec executes does not return an error. One trick is to use the pipe command, assuming your exec call is as follows:

EXEC ('convert a.jpg b.jpg', $output, $return _val);  

Can be changed as follows:

EXEC ('convert a.jpg b.jpg 2>&1', $output, $return _val);    Print_r ($output);  

With 2>&1, the command outputs the error from the execution of the shell to the $output variable, which can be analyzed by outputting the variable.

Note: Exec has 3 parameters, the first is the command to execute, the second is an array, the value of the array is generated after the first command executes, the third parameter executes the state, 0 indicates success, and the others indicate failure.

In PHP There are a total of three functions that can be used to execute external command system,exec,passthru.

PHP CLI Mode

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.