Home > Backend Development > PHP Tutorial > Two ways to pass parameters to php from the command line

Two ways to pass parameters to php from the command line

WBOY
Release: 2016-07-25 09:12:34
Original
1146 people have browsed it

##$argv or $argc
$argv An array containing the arguments passed to the current script when run from the command line. $argv[0] is the script file name.
$argc contains the number of arguments passed to the current script when run from the command line. The script's filename is always passed as an argument to the current script, so the minimum value of $argc is 1.
The two variables are only available when register_argc_argv is turned on.

Note: $argv and $argc must be declared as global variables inside class methods or functions

  1. class A
  2. {
  3. public static function b()
  4. {
  5. var_dump($argv);
  6. var_dump(isset($argv));
  7. }
  8. }
  9. A::b( );
Copy code
  1. printarg();
  2. function printarg(){
  3. global $argc,$argv;
  4. print($argc."parameter n");
  5. print_r($argv);
  6. }
Copy code


##getopt
array getopt ( string $options [, array $longopts ] )
options Each character in this string will be treated as an option character. Options matching the incoming script start 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 array of options. 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 options element "opt" identifies an option --opt.

options may contain the following elements:
1. A single character (no value is accepted)
2. A character followed by a colon (this option requires a value)
3. A character followed by two colons (the value of this option can Option) The value of the
option is the first parameter after the string. It doesn't mind if there are spaces before the value.


Return value:
This function will return the option/parameter pair, and return FALSE on failure.


Note:
Option values ​​do not accept spaces (" ") as separators.
The formats of options and longopts are almost the same, the only difference is that longopts needs to be an array of options (each element is an option), while options needs a string (each character is an option).
Option parsing will terminate at the first non-option found, anything after that will be discarded.



source:php.cn
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