PHP is usually requested by HTTP, you can use Get or post methods to receive parameters, and sometimes need to be under the shell command PHP as a script execution, such as timed tasks. This involves the question of how to pass a reference to PHP under a shell command, usually with three different ways to pass the argument.
use $ARGV or $ARGC parameters to receive
Copy Code code as follows:
<?php
/**
* Use $ARGC $argv Accept Parameters
*/
echo "received {$ARGC} parameters";
Print_r ($ARGV);
Perform
Copy Code code as follows:
[root@dell113 lee]#/usr/local/php/bin/php test.php
1 parameter array received
(
[0] => test.php
)
[root@dell113 lee]#/usr/local/php/bin/php test.php a b c D
5 parameter array received
(
[0] => test.php
[1] => a
[2] => b
[3] => C
[4] => D
)
[Root@dell113 lee]#
second, using the Getopt function
Copy Code code as follows:
<?php
/**
* Using the GETOPT function
*/
$param _arr = getopt (' a:b: ');
Print_r ($param _arr);
Perform
Copy Code code as follows:
[root@dell113 lee]#/usr/local/php/bin/php test.php-a 345
Array
(
[A] => 345
)
[root@dell113 lee]#/usr/local/php/bin/php test.php-a 345-b 12q3
Array
(
[A] => 345
[b] => 12q3
)
[root@dell113 lee]#/usr/local/php/bin/php test.php-a 345-b 12q3-e 3322ff
Array
(
[A] => 345
[b] => 12q3
)
third, prompts the user to enter
Copy Code code as follows:
<?php
/**
* Prompt user input, similar to Python
*/
Fwrite (STDOUT, ' Please enter your blog name: ');
Echo ' The information you entered is: '. fgets (STDIN);
Perform
Copy Code code as follows:
[root@dell113 lee]#/usr/local/php/bin/php test.php
Please enter your blog name: Cloud Habitat Community www.jb51.net
The information you have entered is: Yun-Habitat Community www.jb51.net
You can do the same thing, don't let the user enter empty information
Copy Code code as follows:
<?php
/**
* Prompt user input, similar to Python
*/
$fs = true;
do{
OIF ($FS) {
Fwrite (STDOUT, ' Please enter your blog name: ');
$fs = false;
}else{
Fwrite (STDOUT, ' Sorry, blog name can not be empty, please re-enter your blog name: ');
}
$name = Trim (fgets (STDIN));
}while (! $name);
Echo ' The information you entered is: '. $name. ' \ r \ n ";
Perform
Copy Code code as follows:
[root@dell113 lee]#/usr/local/php/bin/php test.php
Please enter your blog name:
Sorry, blog name can not be empty, please re-enter your blog name: Cloud Habitat Community
The information you have entered is: cloud-dwelling community