Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Introduction: PHP CLI
#1
I thought it was about time I got around to writing some sort of tutorial on this since I use it a lot and others could benefit from knowing about it.



What is PHP CLI?

Most people will be used to running PHP scripts through a Web Browser, but many don't know that the same script is also capable of being run at the command line. The ability to run at the command line showed up in PHP 4.2.0.

May I point out here that PHP is probably not the best choice for creating command line applications as other Languages such as Perl and Python do give slightly better performance at the command line and have some tools built in to solve some common headaches, but i'll continue as there really is no reason not to use PHP CLI and isn't a bad choice, just not the best.

Why use PHP CLI
Often at times we find ourselves using the command line (even in windows) and think something might be better automated or just want to build an application using the command line. Rather than learning a new language and having to constantly switch between languages, it is a lot easier to stick to one language.

So, lets get down to the nitty gritty of it.

NOTE: Throughout the tutorial I will be referring to Terminal but if you are on Windows, this is just your Command Prompt.

First of all, lets check PHP is available from your command line. So fire up Terminal and type

Quote:php -v

which should output something similar to

Quote:PHP 5.3.0 (cli) (built: Jul 19 2009 00:34:29)
Copyright © 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright © 1998-2009 Zend Technologies

If you don't get any output or get an error, you need to install PHP. On Windows there are automated installers, on linux you can download the source and compile it like normal (if you have apt its even simpler as you can just run apt-get install php-cli).

The output should have (cli) after the PHP Version, if you have (cgi) you need to install php-cli.

The CLI Interface is just like any other PHP Script and almost any PHP Script can be run perfectly from the command line. Example hello.php:

PHP Code:
<?php
    
echo "Hello World!";
?>

Now in your Terminal, navigate to where you saved hello.php and run php hello.php. It should output

Quote:Hello World!

Streams
When trying to output text on the command line, echo and print are not the best for two reasons. One being that on Windows, all the text will be outputted when the script finished rather than when you specify (*nix doesn't have this problem). The other reason is that output text in command line applications isn't all the same. It gets split into two groups. Standard Out and Standard Error to make it easier for the system to log errors.

We have three streams available to us.

php://stdin - for reading input
php://stdout - for writing normal output
php://stderr - for writing errors

To write out normal text the proper way, we would change our previous hello world script to this

PHP Code:
<?php
    fwrite
(STDOUT"Hello World!");
?>

When you run the script, you wont notice a difference the previous script, but behind the scenes your script is now outputting to the correct stream.

Now what if we want some input? We now have access to STDIN, which is the Standard input. The following script will ask the user for their name and then say Hello to them. Try running this in the same way we ran the previous scripts.

PHP Code:
<?php
    fwrite
(STDOUT"What is your name? ");

    
//Ask the user for input
    
$name fgets(STDIN);
    
    
//We have to remove a stray new line at the end of the input
    
$name str_replace("\n"""$name);

    
fwrite(STDOUT"Hello $name!\n");

    
//Exit properly
    
exit(0);
?>

When Run it should output like this

Quote:What is your name? Jamza
Hello Jamza!

Looking at the code, you will notice that instead of just taking the raw input, we remove newlines from it. This is because, when the user submits their input using the return key, this gets included in the input and we don't want it.

You will also notice instead of just leaving it to end itself, we put exit(0);[/quote] to end the script. The number in the brackets is used to tell the command line if we are exiting on an error and what the error is. Zero tells the command line the script executed successfully.


Now this is just a short introduction to PHP on the CLI. In the next part of my tutorial I will talk about Command Line Arguments, Errors and more.

I hope this helps Big Grin
Reply
#2
(10-09-2009, 06:34 AM)Jamza Wrote: I hope this helps Big Grin

It doesn't...... j/k Roflmao I already knew this.

Very nice tutorial man and simple to follow,
Thank you for wasting your time to HELP people. Blackhat
Reply
#3
That's nice.Thanks for sharing it to people
[Image: 2d75599e9a.png]:superman:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 941 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,668 10-10-2011, 01:00 PM
Last Post: Greyersting
  Do any of you actually use PHP from the CLI? Orgy 13 3,882 09-05-2011, 12:28 AM
Last Post: AceInfinity

Forum Jump:


Users browsing this thread: 1 Guest(s)