Updated the CLI example to use a proper exit code. Closes #121

This commit is contained in:
Volker Dusch
2012-07-18 15:14:29 +03:00
parent f01b1b860b
commit 37b0c1c95f

View File

@@ -23,7 +23,8 @@ Let's write a simple "Hello, $name" CLI program. To try it out, create a file na
{% highlight php %}
<?php
if($argc != 2) {
die("Usage: php hello.php [name].\n");
echo "Usage: php hello.php [name].\n";
exit(1);
}
$name = $argv[1];
echo "Hello, $name\n";
@@ -31,6 +32,8 @@ echo "Hello, $name\n";
PHP sets up two special variables based on the arguments your script is run with. [`$argc`][argc] is an integer variable containing the argument *count* and [`$argv`][argv] is an array variable containing each argument's *value*. The first argument is always the name of your PHP script file, in this case `hello.php`.
The `exit()` expression is used with a non zero number to let the shell know that the command failed. Commonly used exit codes can be found [here][exit-codes]
To run our script, above, from the command line:
{% highlight bash %}
@@ -50,3 +53,4 @@ Hello, world
[argv]: http://php.net/manual/en/reserved.variables.argv.php
[php-cli]: http://php.net/manual/en/features.commandline.php
[php-cli-windows]: http://www.php.net/manual/en/install.windows.commandline.php
[exit-codes]: http://www.gsp.com/cgi-bin/man.cgi?section=3&topic=sysexits