CHAPTER 16 – Parsing Command-Line Options 2
Exit Code If the script fails, exit with a non-0 code (except 255, which is reserved by PHP itself for compile/parse errors). If the script does not fail, exit with code 0. Be aware that earlier PHP versions (pre-4.2) had a bug in the exit code handling. Exiting in any other way than letting the script finish results in a "non-true" exit code.
Error Messages Prepend the script name to all error messages, so the user can see from which script the error originates. This is useful if the script is invoked from within other scripts or programs so you can see from which program the error originates. If you base your error messages on the PEAR error handling, you can set this up in fire-and-forget mode, like this: $progname = basename($argv[0]); PEAR::setErrorHandling(PEAR_ERROR_DIE, "$progname: %sn"); Here, unless another error handler explicitly overrides the default one, all uncaught PEAR errors will cause the script to die after printing program- name: error message. You can keep coding in the script, resting assured that if there is an error, the default handler will catch it, display the message, and exit, and you don't have to litter your code with error checks.
Process Control When running PHP scripts in CLI, the pcntl extension provides functions for controlling the PHP process. If PHP is embedded in a web server or some- where else, process control is left to the embedding environment and pcntl is disabled.
Processes A process is a piece of code executed by the operating system. On UNIX, processes consist of executable code, environment vari- ables, stack memory, heap (dynamically allocated) memory, file descriptors, and security properties such as user id.
When executing a PHP script, the php process's executable code is the php binary itself (for example, /usr/local/bin/php). The script is stored in heap mem- ory, although both heap and stack memory are used during script execution.