PHP CLI Mode Learning (PHP command line mode) _php Tutorial

Source: Internet
Author: User
Tags php cli php multithreading
Introduction to PHP_CLI Mode

PHP-CLI is the abbreviation for PHP command line interface, as its name implies, is the interface that PHP runs on the command lines, different from the PHP environment (PHP-CGI, ISAPI, etc.) that runs on the Web server, which means PHP can not only write the foreground page, it can also be used to write the background of the program. PHP CLI shell scripts apply to all PHP advantages, making it either support scripts or systems or even with GUI applications on the service side! --Note: PHP_CLI mode is supported under Windows and Linux

PHP-CLI Application Scenario:

1. Multi-threaded applications
The benefits of this, quoting Bird brother's words:

Advantages:
1. Using multiple processes, the kernel is responsible for reclaiming resources after the child process is finished
2. With multiple processes, a child process exception exit does not cause the entire process thread to exit. The parent process also has the opportunity to rebuild the process.
3. A permanent master process, which is responsible for the distribution of tasks, is more logically understood.

PHP Multithreading-Right is the PHP multi-threaded application, although it is widely accepted that PHP is not multi-threading (curl belongs to the simulation of multi-threaded rather than real), but in the PHP_CLI mode of PHP is completely multithreaded. This time PHP belongs to a daemon of Linux. I have written before, "PHP Multi-threaded Batch collection download beautiful pictures (continued)" While in the collection program, although using curl to simulate multi-threading, but in the browser execution time will also encounter the execution timeout or memory abort, resulting in a program interruption, (to try a few times can be completely successful), However, if you execute in PHP-CLI mode, you will find that the program executes quickly and the advantages of PHP multithreaded execution are fully demonstrated.

Note: This multithreaded approach is not very mature, not suitable for large-scale build applications, occasionally used or can be

2. Timed PHP program execution

Before I summarize the three ways of "PHP Timed execution Plan task", using one is the cron way of using Linux, then how to execute PHP program on a regular basis? Please see below

3. Developing desktop Programs

You can do your Windows or Linux in a graphical user interface (GUI) app using PHP! All you need is a PHP command line interface and a packet of GTK. This will allow for the creation of a real portable graphical user interface application (hehe, just know that PHP can do desktop programs, and now only know that you are using PHP_CLI mode), and do not need to learn anything else.

4. Writing a shell script for PHP
What if you don't use bash shell or Perl, but you need some script to execute it? This time you can use your familiar PHP to write shell scripts, this time you do not suddenly feel that PHP is too powerful! --Truly a language, developed everywhere!

PHP_CLI How to use

Win the following method of execution:
Assume that the Php.exe in d:xamppphp on a DOS command can be performed on this:

Copy CodeThe code is as follows: D:\xamppphpphp.exe D:\xampphtdocstest.php

You can execute the test.php file. It is recommended that the win platform under the XAMPP integration environment, really powerful than wamp n times, this integration package can be directly into the DOS mode.

Linux under PHP_CLI use
First find the path to your PHP installation, take me for example:



PHP installed under Path/usr/local/php/bin/php

Copy CodeThe code is as follows:/usr/local/php/bin/php/usr/local/apache/htdocs/a.php

You can execute a. PHP file

PHP_CLI programming needs to know
How to detect environment support PHP_CLI mode?

Copy CodeThe code is as follows:
Method 1
if (Php_sapi = = = ' CLI ')
{
// ...
}
Method 2
if (php_sapi_name () = = = ' CLI ')
{
// ...
}


Php_cli How do I receive parameters?
By default, the/usr/local/php/bin/php receive parameter is $ARGV, and the variable is fixed! Var_dump ($ARGV) in PHP file;

Get the following results:



It is possible to write a simple processing function to transform this method into a get/post parameter pattern commonly used by others.

Simple code:
Copy CodeThe code is as follows:
function Parseargs ($ARGV) {
Array_shift ($ARGV);
$out = Array ();
foreach ($argv as $arg) {
if (substr ($arg, 0,2) = = '--') {
$eqPos = Strpos ($arg, ' = ');
if ($eqPos = = = False) {
$key = substr ($arg, 2);
$out [$key] = Isset ($out [$key])? $out [$key]: true;
} else {
$key = substr ($arg, 2, $eqPos-2);
$out [$key] = substr ($arg, $eqPos + 1);
}
} else if (substr ($arg, 0, 1) = = '-') {
if (substr ($arg, 2, 1) = = ' = ') {
$key = substr ($arg, 1, 1);
$out [$key] = substr ($arg, 3);
} else {
$chars = Str_split (substr ($arg, 1));
foreach ($chars as $char) {
$key = $char;
$out [$key] = Isset ($out [$key])? $out [$key]: true;
}
}
} else {
$out [] = $arg;
}
}
return $out;
}
Var_dump ($ARGV);
Var_dump (Parseargs ($ARGV)); exit;


Execution Result:



Of course, more than one method of implementation, you can try other methods to achieve!

Exceptions to the PHP CLI there are many parameters can be added: for specific reference: http://php.net/manual/en/features.commandline.php

About timed execution
Cron is a timed execution tool under Linux that can run jobs without human intervention, recurring jobs, such as backing up data open/etc/crontab, adding:

Copy CodeThe code is as follows:
/usr/bin/php-f/data/htdocs/test.php


Detailed use of Corntab reference 51cto topic: Linux Scheduled Tasks--cron services

This article references
Http://www.jb51.net/article/1716.htm
Http://www.jb51.net/article/37804.htm
Http://www.jb51.net/article/37796.htm

Note: 2012-06-16 increased PHP_CLI programming need to know, etc.

http://www.bkjia.com/PHPjc/327206.html www.bkjia.com true http://www.bkjia.com/PHPjc/327206.html techarticle Introduction to the PHP_CLI mode PHP-CLI is the abbreviation of PHP command line interface, as its name means, is the interface that PHP runs on the command lines, different from the PHP environment ( ...) running on the Web server.

  • 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.