PHP basic CLI mode development does not require a detailed introduction of any Web server

Source: Internet
Author: User
Tags cli interpreter php basics php cli
PHPCLI mode development does not require any Web server (including Apache or MSIIS), so that CLI can run in various scenarios. There are two ways to run the PHPCLI script. Reprinted, please specify the source: PHP Basics: CLI mode development does not require any Web server

Php cli mode development does not require any Web server (including Apache or ms iis), so that CLI can run on various occasions.
You can run the php cli script in two ways.

The first method is to use php-f/path/to/yourfile. php. Call the php cli interpreter and pass parameters to the script. In this method, you must first set the path of the php interpreter. before running CLI on Windows platform, you must set a command similar to path c: \ php, which also loses the meaning of the first line of the CLI script, therefore, this method is not recommended.

The second method is to first run chmod + x <要运行的脚本文件名> (UNIX/Linux environment), set the php file to executable permissions, and then add the declaration in the first line of the CLI script header (similar #! /Usr/bin/php or php cli interpreter location), and then directly execute the command line. This is the preferred method for CLI. we recommend that you use this method.

Let's take a look at how to compile the php cli script.

1. write the first CLI script
First, create a php script named myfile. PHP to run the php cli. This script is very simple and only displays "Hello php cli !". The script code is as follows:

#! /Usr/local/bin/php-q Do not forget to set the executable permission for this file: $ chmod 755 myfile. php then directly enter the following command and press the Enter key to run: $. /myfile. if you want to run the script in Windows, you can directly run the script without setting file attributes. Microsoft Windows [version 6.0.6000] Copyright (C) 2006 Microsoft Corporation. All rights reserved. C: \> myfile. php Hello php cli!

Again: on the Windows platform, the first line of the client script must write the location of php.exe, as shown in the following figure. (in addition, if you want to add a comment statement to the CLI script, you must write the comment in the PHP tag, because CLI interpretation only recognizes the first line, and does not consider it as a syntax error in the PHP tag ):

#! C: \ php \ php.exe-q

In this way, you can see that the information in the command line has been printed to prove that the CLI script has been successfully run.

2. read parameters from the command line

To obtain parameters from the command line, CLI can obtain the number and value of parameters from $ _ SERVER ['argc '] and $ _ SERVER ['argv. Create another file named testargs. php. the script code is as follows:

#! C: \ php \ php.exe-q Enter the following code in the command line: C: \ Users \ John> testargs. php Always To Be Best test. obtain the parameter: 4 AlwaysToBeBest.

Because we input a string of words, which are "Always To Be Best", the script parameters are separated by spaces. Therefore, PHP counts it as four parameters, which are described below.

The $ _ SERVER ["argc"] array returns an integer value, which indicates that several parameters are input after you press enter from the command line.

As shown in the preceding example, to access the passed parameter value, you must start with index 1. Because the script file occupies index 0, that is, $ _ SERVER ["argv"] [0].

3. process I/O channels

PHP was originally designed not to be used in combination with the user's direct keyboard input or text output. Understanding this design is crucial because any operation on the command line must be able to communicate back and forth with the user.

The input/output (I/O) channel comes from UNIX systems. UNIX systems provide three file handles for sending and receiving data from an application and a user terminal.

We can redirect the output of a script to a file:

php world.php > outputfile

In a UNIX system, you can also use a channel to direct to another command or application. For example:

Php world. php | sort.

In PHP 5 CLI, there is a file stream handle that can use three system constants, namely STDIN, STDOUT, and STDERR. Next we will introduce them separately.

(1) STDIN

STDIN is generally referred to as standard in or standard input. standard input can obtain any data from the terminal.

Format: stdin ('php: // stdin ')

The following example shows the user input:

#!/usr/local/bin/php -q
 

This code works in a similar way as the cat command, which returns all input to it. However, it cannot receive parameters.

STDIN is a standard input device for PHP. using it, cli php scripts can do more. For example:

#! /Usr/local/bin/php-q After the script is executed, it will show: Hello! What is your name (please input): for example, after entering Raymond, it will show: Welcome to Raymond

(2) STDOUT

STDOUT is called standard out or standard output. standard output can be output directly to the screen (or to other programs and obtained using STDIN ), if the print or echo statement is used in php cli mode, the data will be sent to STDOUT.

Format: stdout ('php: // stdout ')

We can also use PHP functions to output data streams. For example:

#!/usr/local/bin/php –q
 

The output result is as follows: Hello World, for example, the echo and print commands are printed to the standard output.

#! /Usr/local/bin/php-qOutput #1.
 Output #1. Output #2. Output #3.

Note:New lines not marked by PHP have been output, but the echo or print command does not indicate line breaks. In fact, the command prompt re-appears in the line where Output #2. Output #3. is located. Any other printing functions of PHP will run normally like this function, and any function written back to a file will do the same.

#!/usr/local/bin/php -q 
 

The above code will explicitly open php: // stdout as the output channel, and php: // output is usually run in the same way as php: // stdout.

(3) STDERR

STDERR is called a standard error. by default, it is directly sent to the user terminal. when the STDIN file handle is used to read data from other applications, an "stdin. stderr" is generated ".

Format: stderr ('php: // stderr ')

The script below shows how to output a line of text to the error stream.

#!/usr/local/bin/php –q
 

PHP 5.2 can directly use STDOUT as a constant, instead of defining the variable $ STDOUT used above. to be compatible with previous versions, we still use custom Variables. if you are using PHP 5.2, you can refer to the second example of STDIN.

4. run CLI in the background

If you are running a process and the process will not end when you exit the account, that is, you can use the nohup command to run it in the background or background of the system. This command can continue running the corresponding process after exiting the account.

Nohup means no hang up in English ). The command is generally in the following format:

Nohup-f scriptname. php &

Use the nohup command to submit a job. by default, all the output of the job is redirected to a file named nohup. out, unless an output file is specified.

Nohup scriptname. php> log.txt &

In this way, the result of PHP cliscript execution will be output to log.txt. we can use the tail command to view the content:

Tail-n50-f log.txt

Now let's implement two examples. The first one is to automatically generate a static HTML file every 10 minutes and keep running. The script code is as follows:

#! /usr/local/bin/php 
 

Save and exit the vi editor, and then grant the executable permission to the genHTML. php file:

#> Chmod 755 genHTML. php then run the script in the background and run the following command: $ nohup genHTML. php-f & the following prompt appears after executing the command: [1] 16623

After you press enter, a shell prompt is displayed. The above prompt indicates that all output information for command execution will be stored in the nohup. out file.

After an authorization command is executed, a specified HTML file, such as article_111990120.html, will be generated in the specified directory every 10 minutes.

How can I terminate the CLI program running in the background?

You can use the kill command to terminate the process. before terminating the process, you must know the PID of the process, that is, the process ID. We use the ps command:

Www # ps pid tt stat time command 561 v0 Is +. 00/usr/libexec/getty Pc ttyv0 562 v1 Is +. 00/usr/libexec/getty Pc ttyv1 563 v2 Is +. 00/usr/libexec/getty Pc ttyv2 564 v3 Is +. 00/usr/libexec/getty Pc ttyv3 565 v4 Is +. 00/usr/libexec/getty Pc ttyv4 566 v5 Is +. 00/usr/libexec/getty Pc ttyv5 567 v6 Is +. 00/usr/libexec/getty Pc ttyv6 568 v7 Is +. 00/usr/libexec/getty Pc ttyv7 16180 p0 I 0: 00. 01 su 16181 p0 S. 06 _ su (csh) 16695 p0 R +. 00 ps 16623 p0 S. 06/usr/local/bin/php/usr/local/www/data/genHTML. php has seen that the PHP process ID is: 16623, so run the kill Command: $ kill-9 16623 [1] + Killed nohup/usr/local/www/data/genHTML. php

At this TIME, the process of this COMMAND has been terminated, and then run the ps COMMAND: $ ps pid tt stat time command 82374 p3 Ss 0: 00. 17-bash (bash) 82535 p3 R +. 00 ps

The php cli script is no longer available. if you cannot see the process by directly running the ps command, you can view it by using the ps & apos commands.

Note:The preceding example must run in UNIX or Linux systems, such as FreeBSD and Redhat Linux. the nohup command is not supported in Windows.

The above is the content that does not require a detailed introduction of any Web server for PHP basic CLI mode development. For more information, see PHP Chinese network (www.php1.cn )!

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.