PHP - Crash Course in CLI
"Bob, you know enough PHP dude, learn something more useful, like Python!"
"Why, Dave? PHP is just as useful!"
"HAR HAR!! You have to waste time making a nice HTML page for your user to take advantage of the PHP code!"
"Um.. no you don't..."
Enter the CLI (Command Line Interface). For this tutorial, I will be using a WAMP package named [URL="http://www.wampserver.com/en/"]WAMPServer[/URL].
First off, you need to make the PHP file. I named mine 'cli.php' and saved it in the directory PHP is installed in. It should look like this.
[PHP]#!C:\wamp\bin\php\php5.2.5\php.exe -q
<?php
echo "Testing PHP CLI";
?>
[/PHP]
Now go to the Command Prompt (Start --> Run --> Type 'cmd' w/out quotes). If your Command Prompt isn't in your PHP directory, type this in the Command Prompt:
cd C:\wamp\bin\php\php5.2.5
// You may have to change the 5.2.5 to your version of PHP
Now type this:
You should get an output of "Testing PHP CLI". Congratulations, you've made your first PHP CLI script!
Now it's time to take off your floaties and get in the big boy pool. We are going to learn how to read user input. I've changed around cli.php to look like this.
[PHP]#!C:\wamp\bin\php\php5.2.5\php.exe -q
<?php
// Simple intro statement
echo "Testing PHP CLI. Type in your name";
// This newline is so the cursor is on the next line
// This isn't really necessary
echo "\n";
// Here we are getting input from
// the STandarD INput stream, this is where the user
// enters input
$input = fgets(STDIN);
// After getting input, we write it to the STandarD OUTput
// stream, so you can see it in your Command Prompt
fwrite(STDOUT, "How are you $input");
?>
[/PHP]
Now run this script. I've done a test run with my input in bold
Testing PHP CLI. Type in your name
Cheese
How are you Cheese
?
This brings up a question. Why did the ? go on the next line? This is because when you were typing in your name, you pressed 'Enter' after finishing. This sent a newline character (\n) to your input stream. So when we output your name, we also output the \n, causing anything after it to move to the next line. Here is a simple remedy for that.
[PHP]#!C:\wamp\bin\php\php5.2.5\php.exe -q
<?php
// Simple intro statement
echo "Testing PHP CLI. Type in your name";
// This newline is so the cursor is on the next line
// This isn't really necessary
echo "\n";
// Here we are getting input from
// the STandarD INput stream, this is where the user
// enters input.
// Notice the use of the trim() function to
// get rid of the newline character in the input
$input = trim(fgets(STDIN));
// After getting input, we write it to the STandarD OUTput
// stream, so you can see it in your Command Prompt
fwrite(STDOUT, "How are you $input?");
?>[/PHP]
Now our output will come out exactly as we want it to.
Last but not least in our CLI crash course, we will learn about something vital to any scripting language: taking arguments from the command line. This is done by a predefined array called $_SERVER['argv']. The first argument (i.e. value in the argv array), $_SERVER['argv'][0], is the actual name of your file. That is why you have to start reading in arguments from $_SERVER['argv'][*]. This will be the start of any actual arguments passed to the script. I have modified my cli.php yet again. Here is what it looks like:
[PHP]#!C:\wamp\bin\php\php5.2.5\php.exe -q
<?php
// Simple intro statement
echo "Testing PHP CLI. I don't want to hear any of these arguments from you!";
// This newline is so the cursor is on the next line
// This isn't really necessary
echo "\n";
// If an argument was provided for this script
if (isset($_SERVER['argv'][*])) {
// We write it to the output stream
fwrite(STDOUT, $_SERVER['argv'][*]);
} else {
fwrite(STDOUT, "You didn't even argue with me!");
}
?>[/PHP]
Now let's run this script.
php cli.php cheese
Testing PHP CLI. I don't want to hear any of these arguments from you!
cheese
If we'd have left out the 'cheese' argument, we would have gotten the "You didn't even argue with me!" message.
Before I conclude, I'd like to add that the echo statement and the fwrite() to STDOUT can pretty much be used interchangeably. Personally, I am just more comfortable using the fwrite() because it gives me that warm fuzzy CLI feeling inside.
Thanks for reading, and I hope you learned something!
~Moonbat
Last edited by Moonbat; 09-02-2008 at 08:08 PM.
"Workers of the world unite; you have nothing to lose but your chains." -Karl Marx