>  기사  >  백엔드 개발  >  PHP 명령줄(CLI 모드)에 대한 자세한 소개

PHP 명령줄(CLI 모드)에 대한 자세한 소개

不言
不言앞으로
2019-01-03 10:32:255358검색
이 글은 PHP 명령줄(CLI 모드)에 대한 자세한 소개를 담고 있습니다. 도움이 필요한 친구들이 참고할 수 있기를 바랍니다.

CLI 모드

CLI 모드는 실제로 명령줄 실행 모드입니다. 영어 이름은 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>的配置信息

대화형 쉘 모드에서 PHP 실행

#🎜🎜 #http: //php.net/manual/en/features.commandline.interactive.php

대화형 셸은 위쪽 및 아래쪽 키를 사용하여 액세스할 수 있는 기록을 저장합니다. 기록은 ~/. php_history 파일.
대화형 쉘 모드는 입력된 기록 명령을 저장하며 위쪽 및 아래쪽 키를 사용하여 액세스할 수 있습니다. 기록은 ~/.php_history 파일에 저장됩니다.

$ 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)

관련 클래스, 확장 또는 기능에 대한 정보 찾기

일반적으로 PHP를 사용할 수 있습니다. info 명령을 사용하거나 웹 서버의 PHP 프로그램에서 phpinfo() 함수를 사용하여 PHP 정보를 표시한 다음 관련 클래스, 확장 또는 함수에 대한 정보를 찾는 것은 정말 번거로운 작업입니다.

$ php --info | grep redis
redis
Registered save handlers => files user redis
This program is free software; you can redistribute it and/or modify

문법 검사

php 스크립트를 실행하지 않고 구문 오류만 확인하면 됩니다. 일부 편집기나 IDE에서 PHP 파일에 구문 오류가 있는지 확인하는 것과 같습니다.

PHP 파일에서만 구문 검사를 수행하려면 -l(--syntax-check)을 사용하세요.

$ php -l index.php
No syntax errors detected in index.php

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

명령줄 스크립트

$argc에는 $argv 배열에 포함된 요소 수가 포함됩니다.# 🎜🎜 #$argv는 제공된 매개변수를 포함하는 배열입니다. 첫 번째 매개변수는 항상 스크립트 파일 이름입니다.

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
#🎜🎜 #

보시다시피 0번째 매개변수는 우리가 실행하는 스크립트의 이름입니다. 제공된 첫 번째 매개변수가 -로 시작하는 경우 앞에 -를 추가하여 다음 매개변수가 PHP 실행 파일이 아닌 스크립트에 제공된다는 것을 PHP에 알려야 합니다(php -r 'var_dump($argv)). ;' -- -시간).

또한 스크립트에서 php_sapi_name() 함수를 사용하여 명령줄에서 실행 중인지 확인할 수 있습니다.


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

위 내용은 PHP 명령줄(CLI 모드)에 대한 자세한 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 cnblogs.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제