php如何进⼊cli模式,PHP命令⾏(CLI模式)的详细介绍本篇⽂章给⼤家带来的内容是关于PHP命令⾏(CLI模式)的详细介绍,有⼀定的参考价值,有需要的朋友可以参考⼀下,希望对你有所帮助。
CLI模式
CLI模式其实就是命令⾏运⾏模式,英⽂全称Command-Line Interface(命令⾏接⼝)
$ php -h
Usage: php [options] [-f] [--] []
php [options] -r [--] []
php [options] [-B ] -R [-E ] [--] []
php [options] [-B ] -F [-E ] [--] []
php [options] -S : [-t docroot] [router]
php [options] -- []
php [options] -a
-a Run as interactive shell
以交互shell模式运⾏
-c | 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 'bar'
定义⼀个INI实体,key为foo,value为'bar'
-e Generate extended information for debugger/profiler
为调试和分析⽣成扩展信息
-f Parse and execute .
解释和执⾏⽂件
-h This help
打印帮助信息
-i PHP information
显⽰PHP的基本信息
-l Syntax check only (lint)
进⾏语法检查(lint)
-m Show compiled in modules
显⽰编译到内核的模块
-r Run PHP without using script tags ..?>
运⾏PHP代码,不需要使⽤标签..?>
-
B Run PHP before processing input lines
在处理输⼊之前先执⾏PHP代码
-R Run PHP for every input line
对输⼊的每⼀⾏作为PHP代码运⾏
-F Parse and execute for every input line
对输⼊的每⼀⾏解析和执⾏
-E Run PHP after processing all input lines
在处理所有输⼊的⾏之后执⾏PHP代码
-H Hide any passed arguments from external tools.
隐藏任何来⾃外部⼯具传递的参数
-S : Run with built-in web server.
运⾏内置的web服务器
-t Specify document root for built-in web server.
指定⽤于内置web服务器的⽂档根⽬录
-s Output HTML syntax highlighted source.
输出HTML语法⾼亮的源码
-v Version number
输出PHP的版本号
php如何运行代码-w Output source with stripped comments and whitespace.
输出去掉注释和空格的源码
-z Load Zend extension .
载⼊Zend扩展⽂件
< Arguments passed to script. Use -- args when first argument
starts with - or script is read from stdin
传递给要运⾏的脚本的参数。当第⼀个参数以'-'开始或者是脚本是从标准输⼊读取的时候,使⽤'--'参数--ini Show configuration file names
显⽰PHP的配置⽂件名
--rf Show information about function .
显⽰关于函数的信息
--rc Show information about class .
显⽰关于类的信息
--re Show information about extension .
显⽰关于扩展的信息
--rz Show information about Zend extension .
显⽰关于Zend扩展的信息
--ri Show configuration for extension .
显⽰扩展的配置信息
以交互式Shell模式运⾏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.
交互shell模式保存输⼊的历史命令,可以使⽤上下键访问到。历史被保存在~/.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命令或者在在web服务器上的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⽂件是否存在语法错误。
使⽤-l(--syntax-check)可以只对PHP⽂件进⾏语法检查。
$ php -l index.php
No syntax errors detected in index.php
假如index.php中存在语法错误。
$ 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
命令⾏脚本
$argc 包含了 $argv数组包含元素的数⽬
$argv 是⼀个数组,包含了提供的参数,第⼀个参数总是脚本⽂件名称
console.php的命令⾏脚本⽂件
echo '命令⾏参数个数: ' . $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);' -- -h)。
另外,在脚本中,我们可以通过php_sapi_name()函数判断是否是在命令⾏下运⾏的。
$ php -r 'echo php_sapi_name(), PHP_EOL;'
cli

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。