Method 1 uses argc, argv
$argc — the number of parameters passed to the script
$argv — passed to Parameter array of the script
1 2 3 4 |
|
Run /usr/local/php/bin/php ./getopt.php -f 123 -g 456 under the command line (recommended learning: PHP programming from entry to proficiency )
1 2 3 4 5 6 7 8 9 |
|
Method 2 uses getopt function ()
1 |
|
Parameter analysis:
options
The string Each character in will be treated as an option character, and options matching the incoming script begin with a single hyphen (-). For example, an option string "x" identifies an option -x. Only a-z, A-Z and 0-9 are allowed.
longopts
Option array. Each element in this array will be treated as an options string, matching the options passed to the script with two hyphens (–). For example, the long option element "opt" identifies an option -opt.
options may contain the following elements:
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
Method 3 Prompts the user for input, and then obtains the input parameters. A bit like C language
1 2 3 |
|
stdout – standard output device (printf(“..”)) is the same as stdout.
stderr - standard error output device
Both output to the screen by default.
But if you use the standard output to a disk file, you can see the difference between the two. stdout is output to a disk file and stderr is to the screen.
Run /usr/local/php/bin/php ./getopt.php from the command line
Run results
1 2 |
|
The above is the detailed content of How to pass parameters to php script. For more information, please follow other related articles on the PHP Chinese website!