25.1.1. ProblemYou want to process arguments passed on the command line. 25.1.2. SolutionLook in $argc for the number of arguments and $argv for their values. The first argument, $argv[0], is the name of script that is being run: if ($argc != 2) { die("Wrong number of arguments: I expect only 1."); } $size = filesize($argv[1]); print "I am $argv[0] and report that the size of "; print "$argv[1] is $size bytes."; 25.1.3. DiscussionIn order to set options based on flags passed from the command line, loop through $argv from 1 to $argc, as shown in Example 25-1. Parsing commmand-line arguments
In this example, the -v and -q arguments are flags that set $verbose and $quiet, but the -c argument is expected to be followed by a string. This string is assigned to $config_file. 25.1.4. See Also25.2 for more parsing arguments with getopt; documentation on $argc and $argv at http://www.php.net/reserved.variables. |