mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-06 05:57:26 +02:00
Updated the CLI example to use a proper exit code. Closes #121
This commit is contained in:
@@ -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
|
Reference in New Issue
Block a user