Apart from building web applications and websites, there are many other tasks that need to be run on the background in the web server. Most of these kinds of tasks may take a few minutes to many hours to complete.
For example, sending a bulk newsletter email to all the subscribers in a mailing list. Can’t we do this in normal PHP through web scripting? Yes, we can do this through normal web scripting. However, this would work well only when there are a few hundreds of subscribers. What if there were a few thousands of subscribers? Usually, any web script would have only a few minutes to execute. This time period is known as maximum execution time. After which, the web server will terminate the web script abnormally.
By default, this maximum execution time would be set as 5 minutes and it may vary depending upon the server settings. In this case, our web script would be able to send only few hundreds of emails within the given time limit. So is it not possible to develop a bulk email sending application in PHP? Yes, here comes the PHP Command Line Scripting.
Unlike web scripts, Command line scripts doesn’t have any maximum execution time limit and they can run as long as they can, unless until the server shuts down. Using command line scripting, we can accomplish many time consuming tasks like taking backup of entire website and databases, transferring files to another server through ftp, and many more.
How to write a command line script in PHP? Will it be as easy as creating a web script? Yes, we can create command line PHP script as we do for web script, but with few little tweaks. We won’t be using any kind of html tags in a command line scripting, as the output is not going to be rendered in a web browser, but displayed in the DOS prompt / Shell prompt. Don’t ever try to use <BR> tag for inserting new line in the command line scripting ;). Instead use \n to output the new line. Let us start with a small command line script to output “Hello World”.
print "Hello World!";
php://stdin (read)These streams are defined as constants namely STDIN, STDOUT AND STDERR from PHP 4.3.0+ CLI version.
<?phpSo these streams are treated as files and we can use normal file functions like fopen(), fread(), fwrite() to interact with these streams.
fwrite(STDOUT, "Hello World!");
<?phpThe third stream STDERR is used to separate the error message from normal output.
fwrite(STDOUT, “Please enter your name\n”);
$name = fgets(STDIN);
fwrite(STDOUT, "Welcome $name");
<?phpIf the file “demo.txt” doesn’t exists, the following error messages will be displayed.
function ErrorHandler($errno, $errstr, $errfile, $errline)
fwrite(STDERR,"$errstr in $errfile on $errline\n");
$fp = fopen("demo.txt","r");
$str = fread($fp,filesize("demo.txt"));
fwrite(STDOUT, "Task completed successfully!");
fopen(demo.txt): failed to open stream: No such file or directory in D:\demo\tmp\error.php on 10To avoid displaying of errors to the end user, we can pipe the output from the script as follows,
filesize(): stat failed for demo.txt in D:\demo\tmp\error.php on 11
fread(): supplied argument is not a valid stream resource in D:\demo\tmp\error.php on 11
fclose(): supplied argument is not a valid stream resource in D:\demo\tmp\error.php on 12
Task completed successfully!
php error.php 2> error.logNow, the user would see only the following message,
Task completed successfully!
The errors would be logged into a file named “error.log” in the directory where the script was executed. The number 2 is the command line handle used to identify STDERR. Note that 1 is handle for STDOUT. Using the > symbol from the command line, we can direct output to a particular location.
Now we have an idea on the basics of PHP Command Line Scripting. Though this article is a small introduction to PHP Command Line Scripting, hope it would have created a tiny spark for those who were unaware of Command Line Scripting in PHP. Wishing these tiny sparks to be ignited to become a flame.
This blog helps you to determine if your business needs a mobile app or not.View Blog...