Home  >  Article  >  Backend Development  >  Introduction to php command line mode

Introduction to php command line mode

尚
forward
2020-03-12 13:16:592296browse

Introduction to php command line mode

php full set line mode, namely php-cli, is called in official documents: CLI SAPI (Server Application Programming Interface, server application programming port). It sounds complicated. In fact, this name is derived from the fact that php was originally a server-side scripting language.

Differences from server mode

The server mode mainly has two working modes: as a web server mode or as a cgi executable program. The former, For example, as a module in apach (such as: php5apache2.dll); the latter as an executable program, such as php-cig. The current replacement is php-fpm (FastCGI Process Manager).

Look at php -fpm configuration. On the server, put a script file with content:

<?php
phpinfo();
?>

Output:

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

Description The configuration file is /etc/php7/conf.d

of /etc/php7/php.ini

Look at the cli mode configuration file again. Run

php -r "phpinfo();"

-r, which means run to run the complete set. The output is:

...
Server API => Command 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,
...

The configuration file path is: /etc/php/7.0/ cli/php.ini and php-fpm are different.

I often hear people say that PHP can only be used as a temporary server script and cannot be used for long-term work, and that security configuration will affect the command line, etc. This is obviously wrong.

Other differences

cli mode defines three constants STDIN, STDOUT, and STDERR; such as: $stderr = fopen('php://stderr', ' w');

CLI SAPI will not change the current directory to the directory where the run script is located.

php as a shell script

has There are two methods to use the php script as a shell script, such as script:
hello.php

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

Method 1, php script parameter

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

Method 2, add

to the php file header
#!/usr/bin/php

Then chmod u .php.cn/course/list/29/type/2.html

The above is the detailed content of Introduction to php command line mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete