search
HomeBackend DevelopmentPHP TutorialDetailed introduction to PHP command line (CLI mode)
Detailed introduction to PHP command line (CLI mode)Jan 03, 2019 am 10:32 AM
php command line

This article brings you a detailed introduction to the PHP command line (CLI mode). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

CLI mode

CLI mode is actually the command line running mode, the full English name is Command-Line Interface (Command Line Interface)

$ php -h
Usage: php [options] [-f] <file> [--] [args...]
   php [options] -r <code> [--] [args...]
   php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
   php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
   php [options] -S <addr>:<port> [-t docroot] [router]
   php [options] -- [args...]
   php [options] -a

  -a               Run as interactive shell
                   以交互shell模式运行
  -c <path>|<file> Look for php.ini file in this directory
                   指定php.ini文件所在的目录
  -n               No configuration (ini) files will be used
                   指定不使用php.ini文件
  -d foo[=bar]     Define INI entry foo with value &#39;bar&#39;
                   定义一个INI实体,key为foo,value为&#39;bar&#39;
  -e               Generate extended information for debugger/profiler
                   为调试和分析生成扩展信息
  -f <file>        Parse and execute <file>.
                   解释和执行文件<file>
  -h               This help
                   打印帮助信息
  -i               PHP information
                   显示PHP的基本信息
  -l               Syntax check only (lint)
                   进行语法检查(lint)
  -m               Show compiled in modules
                   显示编译到内核的模块
  -r <code>        Run PHP <code> without using script tags <?..?>
                   运行PHP代码<code>,不需要使用标签<?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
                   在处理输入之前先执行PHP代码<begin_code>
  -R <code>        Run PHP <code> for every input line
                   对输入的每一行作为PHP代码<code>运行
  -F <file>        Parse and execute <file> for every input line
                   对输入的每一行解析和执行<file>
  -E <end_code>    Run PHP <end_code> after processing all input lines
                   在处理所有输入的行之后执行PHP代码<end_code>
  -H               Hide any passed arguments from external tools.
                   隐藏任何来自外部工具传递的参数
  -S <addr>:<port> Run with built-in web server.
                   运行内置的web服务器
  -t <docroot>     Specify document root <docroot> for built-in web server.
                   指定用于内置web服务器的文档根目录<docroot>
  -s               Output HTML syntax highlighted source.
                   输出HTML语法高亮的源码
  -v               Version number
                   输出PHP的版本号
  -w               Output source with stripped comments and whitespace.
                   输出去掉注释和空格的源码
  -z <file>        Load Zend extension <file>.
                   载入Zend扩展文件<file>

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin
                   传递给要运行的脚本的参数。当第一个参数以&#39;-&#39;开始或者是脚本是从标准输入读取的时候,使用&#39;--&#39;参数

  --ini            Show configuration file names
                   显示PHP的配置文件名

  --rf <name>      Show information about function <name>.
                   显示关于函数<name>的信息
  --rc <name>      Show information about class <name>.
                   显示关于类<name>的信息
  --re <name>      Show information about extension <name>.
                   显示关于扩展<name>的信息
  --rz <name>      Show information about Zend extension <name>.
                   显示关于Zend扩展<name>的信息
  --ri <name>      Show configuration for extension <name>.
                   显示扩展<name>的配置信息

Run PHP in interactive shell mode

http://php.net/manual/en/features.commandline.interactive.php
The interactive shell stores your history which can be accessed using the up and down keys. The history is saved in the ~/.php_history file.
The interactive shell mode saves the entered historical commands and can be accessed using the up and down keys. . The history is saved in the ~/.php_history file.

$ php -a
Interactive shell

php > echo 5+8;
php > function addTwo($n)
php > {
php { return $n + 2;
php { }
php > var_dump(addtwo(2));
int(4)

Usually, we can use the php --info command or on the web server In the PHP program, the function phpinfo() is used to display PHP information, and then to find information about related classes, extensions or functions. This 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

Grammar check

You only need to check whether the php script has syntax errors, without executing it, such as in some editors or IDEs Check the PHP file for syntax errors.

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 index.php.

$ php -l index.php
PHP Parse error:  syntax error, unexpected &#39;echo&#39; (T_ECHO) in index.php on line 3
Parse error: syntax error, unexpected &#39;echo&#39; (T_ECHO) in index.php on line 3
Errors parsing index.php

Command line script

$argc contains the number of elements contained in the $argv array
$argv is an array that contains The parameters provided, the first parameter is always the command line script file of the script file name
console.php

<?php
echo &#39;命令行参数个数: &#39; . $argc . "\n";
echo "命令行参数:\n";
foreach ($argv as $index => $arg) {
    echo "    {$index} : {$arg}\n";
}


$ php console.php hello world
命令行参数个数: 3
命令行参数:
: console.php
: hello
: world

can be seen, the 0th The parameter is the name of the script we execute. It should be noted that if the first parameter provided starts with -, you need to add - in front to tell PHP that the following parameters are provided to our script, not to the PHP execution file (php -r 'var_dump($argv);' -- -h).
In addition, in the script, we can use the php_sapi_name() function to determine whether it is running under the command line.

$ php -r &#39;echo php_sapi_name(), PHP_EOL;&#39;
cli

The above is the detailed content of Detailed introduction to PHP command line (CLI mode). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
11 Best PHP URL Shortener Scripts (Free and Premium)11 Best PHP URL Shortener Scripts (Free and Premium)Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, ReactBuild a React App With a Laravel Back End: Part 2, ReactMar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation SurveyAnnouncement of 2025 PHP Situation SurveyMar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in LaravelNotifications in LaravelMar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)