Hudzilla.org - the homepage of Paul Hudson
Contents > Alternative PHP uses > Command-line scripting Wish List | Report Bug | About Me ]

21.2.3     Your first CLI script

This is NOT the latest copy of this book; click here for the latest version.

Writing CLI scripts is, for the most part, similar to writing PHP scripts for web use - you have all the same functionality available to you as you would when writing for the web. As such, the scripts we're going to look at will specifically highlight special functionality and clever possibilities for the CLI SAPI.

To begin with, we are going to look at argc and argv - two variables oft overlooked in recent times. These two combined allow you to iterate through parameters passed to your script, with parameter 0 being the script itself.

#!/usr/local/bin/php
<?php
    
print "$argc arguments were passed. In order: \n";

    for (
$i = 0; $i <= $argc -1; ++$i) {
        print
"$i: $argv[$i]\n";
    }
?>

Save that in your public HTML directory as cli1.php. Note that you may need to amend the first line depending on where your CLI SAPI resides.

Line one is simply a shebang line (contraction of "sharp" (#) and "bang" (how else could you pronounce an exclamation mark?)) that points to where the CLI SAPI is on your system.

Line three prints the value of $argc out to the system. The usage of this becomes clear in lines four and five; it correlates directly to "the number of parameters passed to the script minus 1". It is minus one because $argv, the values of the parameters that were passed, is a zero-based array like all others in PHP.

In lines four and five, we use $argc to iterate through $argv and output each element as we go. So, with the script saved as cli1.php, run chmod like this:

chmod +x cli1.php

You should now be able to run the script by itself by typing:

./cli1.php

For output, you should see:

1 arguments were passed. In order:
0: ./cli1.php

Try running the script again - this time, pass in random arguments. For example:

./cli1.php --foo --bar baz=wombat

This time, your output should be:

4 arguments were passed. In order:
0: ./cli1.php
1: --foo
2: --bar
3: baz=wombat

And so, as you can see, you have a building block upon which you can base your first shell application.





<< 21.2.2 CLI SAPI differences   21.2.4 Advanced command-line parsing: getopt() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow