Page 1 of 1

PHP Crash Course in CLI Learn the command line interface Rate Topic: -----

#1 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Post icon  Posted 02 September 2008 - 04:11 PM

"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 WAMPServer.

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.

#!C:\wamp\bin\php\php5.2.5\php.exe -q
<?php 
echo "Testing PHP CLI";
?>



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:

Quote

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:

Quote

php cli.php


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.

#!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");
?>



Now run this script. I've done a test run with my input in bold

Quote

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.

#!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?");
?>


Now our output will come out exactly as we want it to. :D
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'][1]. 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:

#!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'][1])) {
	// We write it to the output stream
	fwrite(STDOUT, $_SERVER['argv'][1]);
} else {
	fwrite(STDOUT, "You didn't even argue with me!");
}
?>


Now let's run this script.

Quote

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

Is This A Good Question/Topic? 0
  • +

Page 1 of 1