diff --git a/reference/info/functions/getopt.xml b/reference/info/functions/getopt.xml index 4661110379..2f4527198e 100644 --- a/reference/info/functions/getopt.xml +++ b/reference/info/functions/getopt.xml @@ -1,5 +1,5 @@ - + getopt @@ -14,9 +14,7 @@ arraylongopts - Returns an associative array of option / argument pairs based on the - options format specified in options, or &false; - on an error. + Parses options passed to the script. @@ -27,33 +25,70 @@ options - - The options parameter may contain the following - elements: individual characters, and characters followed by a colon to - indicate an option argument is to follow. For example, an option string - x recognizes an option -x, and an - option string x: recognizes an option and argument - -x argument. It does not matter if an argument has - leading white space. - + + Each character in this string will be used as option characters and + matched against options passed to the script starting with a single + hyphen (-). + + + For example, an option string "x" recognizes an + option -x. + longopts - - + + An array of options. Each element in this array will be used as option + strings and matched against options passed to the script starting with + two hyphens (--). + + + For example, an longopts element "opt" recognizes an + option --opt. + + + + Prior to PHP5.3.0 this parameter was only available on few systems + + + + The options parameter may contain the following + elements: + + Individual characters (do not accept values) + Characters followed by a colon (parameter requires value) + Characters followed by two colons (optional value) + + Option values are the first argument after the string. It does not matter + if a value has leading white space or not. + + + Optional values do not accept " " (space) as a seperator. + + + + + + The format for the options and + longopts is almost the same, the only difference is + that longopts takes an array of options (where each + element is the option) where as options takes a + string (where each character is the option). + + &reftitle.returnvalues; - This function will return an array of option / argument pairs. If an - option does not have an argument, the value will be set to &false;. + This function will return an array of option / argument pairs or &false; on + failure. @@ -84,25 +119,125 @@ &reftitle.examples; - + <function>getopt</function> Example ]]> + + Running the above script with php script.php -fvalue -h + will output: + + + + string(5) "value" + ["h"]=> + bool(false) +} +]]> + + + + + + <function>getopt</function> Example#2 + + +]]> + + + Running the above script with php script.php -f "value for f" -v + -a --required value --optional="optional value" --option + will output: + + + + string(11) "value for f" + ["v"]=> + bool(false) + ["a"]=> + bool(false) + ["required"]=> + string(5) "value" + ["optional"]=> + string(14) "optional value" + ["option"]=> + bool(false) +} +]]> + + + + + + <function>getopt</function> Example#3 + Passing multiple options as one + + +]]> + + + Running the above script with php script.php -aaac + will output: + + + + array(3) { + [0]=> + bool(false) + [1]=> + bool(false) + [2]=> + bool(false) + } + ["c"]=> + bool(false) +} +]]> + -