PHP command line
Detailed explanation of php command line (CLI) parameters
Run php in interactive shell mode
Run the built-in web server
Find the PHP configuration file
View classes/functions/ Extended information
Syntax check
Command line script development
PHP as a As a web development language, we usually run PHP in the Web Server and access it using a browser, so we rarely pay attention to its command line operations and the use of related parameters. However, especially on Unix-like operating systems, PHP can As a scripting language, it performs processing tasks similar to those of a shell.
Detailed explanation of php command line (CLI) parameters
To view all command line parameters of PHP, use the php -h command. We will explain most of the commonly used command line parameters one by one to deepen our understanding of PHP's capabilities, use PHP on the server command line more quickly or debug various problems that arise due to unfamiliarity with the environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
The above lists all the parameters of the PHP command and their comments. Next, we will give examples of the more commonly used parameters.
Run php in interactive shell mode
Friends who have used Python are familiar with Python’s interactive shell. Under the command line, if we directly enter the python command, we will enter python. Interactive shell program, you can then perform some computing tasks interactively.
In the PHP command line, similar functions are also provided. Use the -a parameter to enter interactive shell mode.
In this shell, we can perform some simple tasks without always creating a new php file.
For more detailed instructions, please refer to the official documentation
Running the built-in Web server
Starting from PHP 5.4.0, PHP’s command line mode provides a built-in Built web server. Use -S to start running the web service.
Assume that we are currently in the directory /Users/mylxsw/codes/php/aicode/demo. In this directory, there is an index.php file.
1 2 3 4 |
|
In this directory, execute the following command to start the built-in web server, and use the current directory as the working directory by default
1 2 3 4 5 |
|
We open another shell window, Request http://localhost:8000/ to see the script output
1 2 3 4 5 6 7 8 |
|
In the window where the web service is running, you can see the output log information
Above we are starting When building the server, only the -S parameter was specified to let PHP run as a web server. At this time, PHP will use the current directory as the working directory, so return to the current directory to find the requested file. We can also use the -t parameter. Specify another directory as the working directory (document root).
For more details, please refer to the official documentation.
Find the PHP configuration file
Sometimes, due to the confusion of software installation on the server, we may have installed multiple versions of the PHP environment. At this time, how to locate our PHP program? Which configuration file is used is more important. In the PHP command line parameters, the –ini parameter is provided. Using this parameter, you can list the current PHP configuration file information.
1 2 3 4 5 6 7 8 9 10 11 |
|
We have installed two versions of PHP on the above server. As you can see from the above, using the php –ini command can easily locate which configuration file the current PHP command will use.
View class/function/extension information
Usually, we can use the php –info command or use the function phpinfo() in the php program on the web server to display php information, and then Finding information about related classes, extensions or functions is really troublesome.
1 2 3 4 |
|
We can use the following parameters to view this information more conveniently
1 2 3 4 5 |
|
For example, we want to view the configuration information of extended redis
1 2 3 4 5 6 |
|
View redis class information
1 2 3 4 5 6 7 8 9 10 11 |
|
View function printf information
1 2 3 4 5 6 7 8 |
|
Syntax check
Sometimes, we only need to check whether the php script exists Syntax errors without executing it, such as checking PHP files for syntax errors in some editors or IDEs.
Use -l (--syntax-check) to perform syntax check only on PHP files.
1 2 |
|
If there is a syntax error in our index.php at this time
1 2 3 4 5 |
|
Command line script development
When using PHP to develop command line scripts At the same time, it is obviously different from developing web programs. In web programs, we can provide different inputs for the PHP environment by changing the parameters of the URL. But how to obtain external input in the command line script program?
在使用C语言开发程序时,我们通常会在main函数中提供两个可选的参数int main(int argc, char *argv[]),这两个参数就是从命令行提供的输入参数。在PHP中,提供了两个全局变量$argc和$argv用于获取命令行输入。
$argc 包含了 $argv数组包含元素的数目
$argv 是一个数组,包含了提供的参数,第一个参数总是脚本文件名称
假设我们有一个名为console.php的命令行脚本文件
1 2 3 4 5 |
|
在命令行下执行该脚本
1 2 3 4 5 6 |
|
可以看到,第0个参数是我们执行的脚本名称。需要注意的是,如果提供的第一个参数是以-开头的话,需要在前面增加–,以告诉php这后面的参数是提供给我们的脚本的,而不是php执行文件的(php -r ‘var_dump($argv);’ — -h)。
另外,在脚本中,我们可以通过php_sapi_name()函数判断是否是在命令行下运行的
1 2 |
|
参考文献
Using PHP from the command line
The above is the detailed content of PHP command line. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
