Ver también

User Contributed Notes 14 notes

"phpnotes at kipu dot co dot uk" and "tim at digicol dot de" are both wrong or misleading. Sean was correct. Quoted space-containing strings on the command line are one argument. It has to do with how the shell handles the command line, more than PHP. PHP's getopt() is modeled on and probably built upon the Unix/POSIX/C library getopt(3) which treats strings as strings, and does not break them apart on white space.

Sometimes you will want to run a script from both the command line and as a web page, for example to debug with better output or a command line version that writes an image to the system but a web version that prints the image in the browser. You can use this function to get the same options whether passed as command line arguments or as $_REQUEST values.

Here's another way of removing options found by getopt() from the argv[] array. It handles the different kind of parameters without eating chunks that do not belong to an --option. (-nr foo param1 param2 foo)<?php$parameters = array('n' => 'noparam','r:' => 'required:','o::' => 'optional::',);

Although very interesting, koenbollen at gnospamail dot com's update of the argv array fails when option values follow the option with no space :Indeed php MyScript.php5 -t5 and php MyScript.php5 -t 5with $options="t:" are treated as the same by getopt.

There are 2 simpler (and much faster) methods for getting good getopt() operation without creating your own handler.

1. Use the Console_Getopt PEAR class (should be standard in most PHP installations) which lets you specify both short and long form options as well as whether or not arguments supplied to an option are themselves 'optional'. Very simple to use and requires very little code to operate compaired to writing own handler.

2. If you cannot load external PEAR objects, use your shell's getopt() functions (which in BASHs case work very well) to process options and have your shell script then call your PHP script with a rigid argument structure that is very easy for PHP to digest such as: % myfile.php -a TRUE -b FALSE -c ARGUMENT ... If the initial arguments are invalid you can have the shell script return an error without calling the PHP script. Sounds convoluted but is a very simple solution and in fact PHP's own % pear command uses this method. /usr/bin/pear is a shell script that does some simle checking before calling pearcmd.php and repassing the arguments on to it.

The second method is by far the best for portability because it allows a single shell script to check a few things like your PHP version and respond acordingly e.g. does it call your PHP4 or PHP5 compatible script? Also, because getopt() is not available on Windows, The second solution allows you to do Windows specific testing as a BAT file (as oposed to BASH, ZSH or Korn on UNIX).

About getopt(String): Parses the command-line arguments into an associative array, using the function's String parameter to specify arguments and options, thus:* arguments are specified as any letter followed by a colon, e.g. "h:".* arguments are returned as "h" => "value".* options are specified as any letter not followed by a colon, e.g. "r".* options are returned as "r" => (boolean) false.

Also note that:1) Options or arguments not passed in the command-line parameters are not set in the returned associative array.2) Options or arguments present in the command-line arguments multiple times are returned as an enumerated array within the returned associative array.