Several methods of PHP passing values in CLI mode
PHP itself is a scripting language that can be used efficiently in command-line environments, but how does the CLI (command line Interface) pass in a value?
Here are the 3 ways I know.
A. getopt (Note: PHP 4 >= 4.3.0, PHP 5)
We set up the following documents: test.php
$opt = getopt (' m:n: ');//$value _m= $opt [' m '];//$value _n= $opt [' n '];p rint_r ($opt);
Then run the command
PHP test.php-mvaluem-n Value N
The results are as follows:
For details, please see the manual: http://www.php.net/manual/zh/function.getopt.php
Two. $argv
We modified the test.php file just now, the content is changed to read as follows:
if ($ARGC > 1) {var_dump ($ARGV);}
Run
PHP test.php 1 2 C
The results are as follows:
For details see the manual: http://www.php.net/manual/zh/reserved.variables.argv.php
Three. Linux stdin
In Linux
StdOut standard output, the default is the terminal stdin standard input, the default is the keyboard stderr standard error output, the default is the terminal
So, we modified the test.php file just now.
Fwrite (STDOUT, "Enter Your Name:"), $name = Trim (fgets (STDIN)); Fwrite (STDOUT, "Hello, $name!");
We run
PHP test.php
At this point the screen output: Enter Your name:
We enter in the back: Tiyee and then enter
The screen will now appear hhellow,tiyee