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.
-a 以交互式shell模式运行 -c | 指定php.ini文件所在的目录 -n 指定不使用php.ini文件 -d foo[=bar] 定义一个INI实体,key为foo,value为'bar' -e 为调试和分析生成扩展信息 -f 解释和执行文件. -h 打印帮助 -i 显示PHP的基本信息 -l 进行语法检查 (lint) -m 显示编译到内核的模块 -r 运行PHP代码,不需要使用标签 ..?> -B 在处理输入之前先执行PHP代码 -R 对输入的没一行作为PHP代码运行 -F Parse and execute for every input line -E Run PHP after processing all input lines -H Hide any passed arguments from external tools. -S : 运行内建的web服务器. -t 指定用于内建web服务器的文档根目录 -s 输出HTML语法高亮的源码 -v 输出PHP的版本号 -w 输出去掉注释和空格的源码 -z 载入Zend扩展文件 . args... 传递给要运行的脚本的参数. 当第一个参数以-开始或者是脚本是从标准输入读取的时候,使用--参数 --ini 显示PHP的配置文件名 --rf 显示关于函数 的信息. --rc 显示关于类 的信息. --re 显示关于扩展 的信息. --rz 显示关于Zend扩展 的信息. --ri 显示扩展 的配置信息.
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.
$ ls index.php $ cat index.php <?php echo "Hello, PHPER!";
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
$ php -S localhost:8000 PHP 5.6.3 Development Server started at Wed Jun 10 15:49:41 2015 Listening on http://localhost:8000 Document root is /Users/mylxsw/codes/php/aicode/demo Press Ctrl-C to quit.
We open another shell window, Request http://localhost:8000/ to see the script output
$ curl -is http://localhost:8000/ HTTP/1.1 200 OK Host: localhost:8000 Connection: close X-Powered-By: PHP/5.6.3 Content-type: text/html; Hello, PHPER!
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.
$ php --ini Configuration File (php.ini) Path: /usr/local/etc/php/5.6 Loaded Configuration File: /usr/local/etc/php/5.6/php.ini Scan for additional .ini files in: /usr/local/etc/php/5.6/conf.d Additional .ini files parsed: (none) $ /usr/local/php/bin/php --ini Configuration File (php.ini) Path: /usr/local/php/etc/ Loaded Configuration File: /usr/local/php/etc/php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none)
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.
$ php --info | grep redis redis Registered save handlers => files user redis This program is free software; you can redistribute it and/or modify
We can use the following parameters to view this information more conveniently
--rf 显示关于函数 的信息. --rc 显示关于类 的信息. --re 显示关于扩展 的信息. --rz 显示关于Zend扩展 的信息. --ri 显示扩展 的配置信息.
For example, we want to view the configuration information of extended redis
$ php --ri redis redis Redis Support => enabled Redis Version => 2.2.7
View redis class information
$ php --rc redis Class [ class Redis ] { - Constants [19] { Constant [ integer REDIS_NOT_FOUND ] { 0 } ... - Methods [201] { ... Method [ public method echo ] { } ...
View function printf information
$ php --rf printf Function [ function printf ] { - Parameters [2] { Parameter #0 [ $format ] Parameter #1 [ ...$args ] } }
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.
$ php -l index.php No syntax errors detected in index.php
If there is a syntax error in our index.php at this time
$ php -l index.php PHP Parse error: syntax error, unexpected 'echo' (T_ECHO) in index.php on line 3 Parse error: syntax error, unexpected 'echo' (T_ECHO) in index.php on line 3 Errors parsing index.php
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的命令行脚本文件
<?php echo '命令行参数个数: ' . $argc . "n"; echo "命令行参数:n"; foreach ($argv as $index => $arg) { echo " {$index} : {$arg}n"; }
在命令行下执行该脚本
$ php console.php hello world 命令行参数个数: 3 命令行参数: 0 : console.php 1 : hello 2 : world
可以看到,第0个参数是我们执行的脚本名称。需要注意的是,如果提供的第一个参数是以-开头的话,需要在前面增加–,以告诉php这后面的参数是提供给我们的脚本的,而不是php执行文件的(php -r ‘var_dump($argv);’ — -h)。
另外,在脚本中,我们可以通过php_sapi_name()函数判断是否是在命令行下运行的
$ php -r 'echo php_sapi_name(), PHP_EOL;' cli
参考文献
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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software