(PHP 4 >= 4.3.0, PHP 5, PHP 7)
getopt — �������в����б��л�ȡѡ��
$options
[, array $longopts
[, int &$optind
]] ) : array��������ű���ѡ�
optionslongoptsoptindoptind parameter is present, then the
index where argument parsing stopped will be written to this variable.
options ���ܰ���������Ԫ�أ�
Note: ѡ���ֵ�����ܿո�" "����Ϊ�ָ�����
Note:
options��longopts�ĸ�ʽ������һ���ģ�Ψһ�IJ�֮ͬ����longopts��Ҫ��ѡ������飨ÿ��Ԫ��Ϊһ��ѡ�����options��Ҫһ���ַ�����ÿ���ַ��Ǹ�ѡ���
�˺����᷵��ѡ��/�����ԣ� ������ʧ��ʱ���� FALSE��
Note:
ѡ��Ľ�������ֹ���ҵ��ĵ�һ����ѡ�֮����κζ������ᱻ������
| �汾 | ˵�� |
|---|---|
| 7.1.0 |
��� optind ������
|
| 5.3.0 | ֧�� "=" ��Ϊ ������ֵ�ķָ����� |
| 5.3.0 | �����˿�ѡֵ��֧�֣���"::"ָ������ |
| 5.3.0 |
���� longopts ������ϵͳƽ̨�Ͼ����á�
|
| 5.3.0 | �˺������������ڲ���ϵͳ������Ҳ�ܹ��� Windows �����С� |
Example #1 getopt() ���ӣ������÷�
<?php
// Script example.php
$options = getopt("f:hp:");
var_dump($options);
?>
shell> php example.php -fvalue -h
�������̻������
array(2) {
["f"]=>
string(5) "value"
["h"]=>
bool(false)
}
Example #2 getopt() ���ӣ����볤ѡ��
<?php
// Script example.php
$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);
?>
shell> php example.php -f "value for f" -v -a --required value --optional="optional value" --option
�������̻������
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)
}
Example #3 getopt() ���ӣ�����ͬһ���ѡ��
<?php
// Script example.php
$options = getopt("abc");
var_dump($options);
?>
shell> php example.php -aaac
�������̻������
array(2) {
["a"]=>
array(3) {
[0]=>
bool(false)
[1]=>
bool(false)
[2]=>
bool(false)
}
["c"]=>
bool(false)
}
Example #4 getopt() ���ӣ�ʹ�� optind
<?php
// Script example.php
$optind = null;
$opts = getopt('a:b:', [], $optind);
$pos_args = array_slice($argv, $optind);
var_dump($pos_args);
shell> php example.php -a 1 -b 2 -- test
�������̻������
array(1) {
[0]=>
string(4) "test"
}