Home > Backend Development > PHP Tutorial > Introduction to php command line mode

Introduction to php command line mode

Release: 2023-04-08 13:08:02
forward
2335 people have browsed it

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();
?>
Copy after login

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
...
Copy after login

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();"
Copy after login

-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,
...
Copy after login

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);
?>
Copy after login

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"
}
Copy after login

Method 2, add

to the php file header
#!/usr/bin/php
Copy after login

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!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template