PHP Command Line Scripting

PHP is one of the open source language widely used to develop web applications. Most of web developers knows but many of us are unaware of PHP command line script. There are many other mundane tasks that can be only possible in the background on web server. Here comes the importance that why we use php in CLI mode, like cron jobs or time consuming task as taking daily backups of files or running such scripts that can run as they can until the web server shutdown.

This article describes how php interacts with CLI (Command Line Interpreter) and run the php program.
In my previous post I gave you a basic knowledge of how to run php in command prompt. No problem if you forgot. simply add the php bin directory to environment variable:
Open computer properties advance options there is PATH variable.
To execute php on CLI (Command Line Interpreter) mode, add php to environment variable
as  ;C:/wamp/bin/php/php5.2.8/bin
Now Reboot the System to set all environment variables.
To test PHP is working on CLI type the following command in prompt

C:/>php  –v  //Gives PHP version
PHP 4.3.6 (cli) (built: Apr 19 2004 10:02:14)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies
To get more help use the switch –h, like
C:/> php -h

Now create a new file named as cli.php and type this program

 print "PHP CLI World!";

You can execute this script by typing:
C:/>php phpcli.php
It display
PHP CLI World!
In command line we cannot render HTML tags, like to insert new line we have to use “\n” instead of “<br>”.

echo "Hello CLI<br>How are You?\n Welcome 2010.";

it display
Hello CLI<br>How are You?  //<br> is as it is printed.
Welcome 2010. // this is in new line

We can also access command line arguments by using $argc, $arg variable or $_SERVER[‘argc’], $_SERVER[‘argv’], access these arguments create a php file and type these lines

echo "argc values: ".$argc;
echo "\n argv values:".$argv[1];

Now in command line run this script. Suppose you have create a file name argtest.php then type
C:/>php argtest.php testarg
We will get

argc values: 2
argv values:testarg

$argc give the number arguments enter including.  $argv is an array of arguments. As you can see file name itself is at index 0 and you can get by $argv[0].
Now lets have a basic idea of Input/output Stream in PHP. There are three IO streams in php.
These streams are defined as constants namely STDIN, STDOUT AND STDERR from PHP 4.3.0+ CLI version.
Now instead of using print or echo for output, we can use the standard output stream defined in CLI version of the PHP.
For an example..

fwrite(STDOUT, "Enter Name:\n");
$name = fgets(STDIN);
fwrite(STDOUT, "Happy Morning $name");

In this example it asks for input, suppose you have entered “Sah”. It save it in $name variable, which is output as “Happy Morning Sah”The third stream STDERR is used to separate the error message from normal output and use as

fwrite(STDERR, "ERROR");

So these are the basics that how php interacts with command line scripting and hope PHP GUI developers now have an idea of PHP when run on terminal is more powerful language than other languages.

One Response to PHP Command Line Scripting

  1. Vijay Joshi says:

    Nice one. Look forward to more articles on PHP cli

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

nike tn pas cher tn pas cher peuterey outlet piumini peuterey outlet spaccio peuterey outlet peuterey outlet piumini peuterey outlet spaccio peuterey outlet hogan outlet scarpe hogan outlet hogan outlet online peuterey outlet piumini peuterey outlet spaccio peuterey outlet hogan outlet scarpe hogan outlet hogan outlet online hogan outlet scarpe hogan outlet hogan outlet online doudoune moncler pas cher moncler pas cher doudoune moncler hogan outlet scarpe hogan outlet hogan outlet online nike tn pas cher tn pas cher