Php-cli Mode Learning (PHP command line mode)

Source: Internet
Author: User
Tags php cli php multithreading
Php-cli Mode Learning (PHP command line mode)

Previously know that the PHP?CLI mode is a shell-like execution PHP program, but always thought this is a backward way, it should be meaningless, because never encountered the use of this CLI mode programming. Today, however, an application that uses CLI mode is encountered.
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 server!?? Note: PHP_CLI mode is supported under both Windows and Linux
PHP-CLI Application Scenario:
1. Multi-threaded applications

The benefits of this, quoting Bird brother's words:

Pros: 1. With 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? Yes, PHP is a multithreaded application, although it is widely accepted that PHP is not multi-threading (curl belongs to analog multithreading rather than real), but PHP in PHP_CLI mode 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 fully use your familiar PHP to write shell scripts, this time you are not suddenly feel PHP is too powerful!?? Really do a language, everywhere development!
Php_cli How to use
Win the following method of execution:

Assume that the Php.exe in D:\xampp\php on a DOS command can be performed on this:
1 D:\xampp\php\php.exe D:\xampp\htdocs\test.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:

Image

PHP installed under Path/usr/local/php/bin/php
1/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?
1 2//Method 1
3 if (Php_sapi = = = ' CLI ')
4 {
5//...
6}
7//Method 2
8 if (php_sapi_name () = = = ' CLI ')
9 {
10//...
11}

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:

Image

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:
1 2
3 function Parseargs ($ARGV) {
4 Array_shift ($ARGV);
5 $out = array ();
6 foreach ($argv as $arg) {
7 if (substr ($arg, 0,2) = = '--') {
8 $eqPos = Strpos ($arg, ' = ');
9 if ($eqPos = = = False) {
Ten $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);
15}
+ 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;
25}
26}
+} else {
$out [] = $arg;
29}
30}
return $out;
32}
Var_dump ($ARGV);
Var_dump (Parseargs ($ARGV)); exit;

Execution Result:

Image

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:

/usr/bin/php-f/data/htdocs/test.php

For more information on the use of Corntab 51cto topic: Linux Scheduled Tasks?? Cron Service
  • 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.