School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,102 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,021 people online right now. Registration is fast and FREE... Join Now!




PHP Crash Course in CLI

 
Reply to this topicStart new topic

> PHP Crash Course in CLI, Learn the command line interface

Moonbat
Group Icon



post 2 Sep, 2008 - 03:11 PM
Post #1


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

CODE
#!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! smile.gif

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.

CODE
#!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.

CODE
#!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. biggrin.gif
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:

CODE
#!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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 12:23PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month