Use command:
-
- php script.php arg1 arg2 arg3
-
Copy code
will output:
-
- $options = getopt("f:hp:");
- var_dump($options);
- ?>
Copy code
Copy the code Code example:
-
- Use the command:
- php script.php -f value -h or php script.php -fvalue -h
Copy the code
and the output will be:
-
-
$shortopts = ""; - $shortopts .= "f:"; // Required value
- $shortopts .= "v::"; // Optional value
- $shortopts .= "abc"; // These options do not accept values
$longopts = array(
- "required:", // Required value
- "optional::", / / Optional value
- "option", // No value
- "opt", // No value
- );
- $options = getopt($shortopts, $longopts);
- var_dump($options);
- ?>< /p>
php script.php -f "value for f" -v -a --required value --optional="optional value" --option will output:
-
-
Copy code
output:
array(6) {
["f"]=>
string(11) "value for f"
["v"]=>
bool(false)
["a"]=>
bool(false)
["required"]=>
string(5) "value"
["optional"]=>
string(14) "optional value"
["option"]=>
bool(false)
}
With the above introduction, I hope it will help everyone understand and master the usage of getopt in php. Programmer's Home, I wish you all learning and progress.
|