关于php命令行模式介绍-php教程

资源魔 25 0

php选集行模式,即php-cli,民间文档中称为: CLI SAPI(Server Application Progra妹妹ing Interface,效劳端使用编程端口).听着挺复杂。实际上是由于php本来为效劳器真个剧本言语,以是引伸出这个叫法。

与效劳端模式的没有同

效劳端模式次要有两种工作形式: 作为web server的模式形式或作为一个cgi可执行顺序. 前者,比方作为apach中的一个模块(如:php5apache2.dll); 后者作为可执行顺序,如php-cig. 如今的代替者为php-fpm(FastCGI Process Manager).

看下php-fpm的设置装备摆设。 正在效劳器上,放一剧本文件,内容:

<?php
phpinfo();
?>

输入:

...
Server API	FPM/FastCGI
Virtual Directory Support	disabled
Configuration File (php.ini) Path	/etc/php7
Loaded Configuration File	/etc/php7/php.ini
Scan this dir for additional .ini files	/etc/php7/conf.d
...

阐明设置装备摆设文件为 /etc/php7/php.ini的/etc/php7/conf.d

再看下cli模式的设置装备摆设文件. 运转

php -r "phpinfo();"

-r 即 run运转选集意义. 输入为:

...
Server API => Co妹妹and Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php/7.0/cli
Loaded Configuration File => /etc/php/7.0/cli/php.ini
Scan this dir for additional .ini files => /etc/php/7.0/cli/conf.d
Additional .ini files parsed => /etc/php/7.0/cli/conf.d/10-opcache.ini,
...

设置装备摆设文件门路为: /etc/php/7.0/cli/php.ini 以及php-fpm是没有同的。

常听到有人说,php只能作为效劳器临时间剧本,不克不及作为长期工作,另有平安设置装备摆设会影响饬令行等,显然是谬误的。

其它差别

cli模式,界说了STDIN, STDOUT, STDERR三个常量; 如: $stderr = fopen(‘php://stderr’, ‘w’);

CLI SAPI 没有会将以后目次改成已运转的剧本所正在的目次.

php作为shell剧本

有两种办法将php剧本作为shell剧本, 如剧本:
hello.php

<?php
echo "hello world!";
var_dump($argv);
?>

办法1, php 剧本 参数

~php hello.php -s 'me'
hello world
array(3) {
  [0]=>
  string(9) "hello.php"
  [1]=>
  string(2) "-s"
  [2]=>
  string(2) "me"
}

办法2, 正在php文件头加

#!/usr/bin/php

而后 chmod u+x hello.php
执行 ./hello.php

hello world
array(1) {
  [0]=>
  string(11) "./hello.php"
}

相干保举:

PHP视频教程:https://www.php.cn/course/list/29/type/2.html

以上就是对于php饬令行模式引见的具体内容,更多请存眷资源魔其它相干文章!

标签: php php开发教程 php开发资料 php开发自学

抱歉,评论功能暂时关闭!