568 Part IV: Using PHP for Sysadmin Tasks (Vps web hosting)
568 Part IV: Using PHP for Sysadmin Tasks Notice that arguments that do not have a value (such as -h) have a value of 1 in the $cmd array. Since 1 evaluates to TRUE in Boolean expressions, when this array is returned, you can easily find out which command-line arguments were selected. In this script, it uses the built-in isset() function to determine whether a known argument is set, and prints a message indicating the result. For example: ./cmd_options.php -h Returns: You selected help option. This shows that -h has been supplied. To add a new short argument called -z, we can simply update the $CMD_SHORT_OPTIONS list. Similarly, to add a long argument called -zero, we can add this argument in $CMD_LONG_OPTIONS. For example, to allow a new long argument called -count that expects a value, we can add count= in the following array: $CMD_LONG_OPTIONS = array( help , size= , count= ); Then we can write code to determine whether the user has chosen this argument or not. For example: if (isset($cmd[ count ])) { echo You entered count value of . $cmd[ count ] . n ; } Now that you know how to deal with command line PHP scripting along with argument handling, let s develop some interesting command-line scripts. In the following section, you will develop a simple reminder tool. Most chapters in this book refer you to the accompanying CD-ROM for source listings. Because the tools discussed in this chapter are very small, we include the source listing so that you can see the entire code while reading the chapters.