Have you ever wondered if it would be possible to execute PHP files from the command line, just like C or Pascal programs?
Besides being kind of fun, you’ve probably actually needed to do this at some point and didn’t know how. Imagine a PHP file that would run when the server started (or your locally installed server) and would take care of deleting old forum messages or updating the statistics of a file manager.
We could create a Crontab if we were using Cpanel or Unix, but sometimes we need to go further. In PHP there is another option currently called CLI (Command Line Interface), which we’ll explore in this article.
The CLI
Since PHP version 4.3.0, the CLI has become an official SAPI (Server API) and now has its own .exe for running command-line applications.
It’s worth noting that PHP is not the only choice for creating command-line applications, but it has obvious advantages. If we’re programming a portal we could use another language like PERL or Python; the problem arises when we’re not very familiar with the language, or even if we are, it would require extra effort to run two different languages on the same server and deal with the possibility of having to fix problems in two different languages. In the vast majority of cases, simplicity is preferable to the performance gains other languages offer.
What kinds of problems can be solved using PHP in the console? We can solve administrative problems where we don’t need to create elaborate visual interfaces, but need to run backups or clean up comment lists. We can also think about making a command-line installation program; sometimes we don’t have folders with write access and this is a good solution.
PHP Version
We can run any version of PHP in the console, but it’s advisable to use version 4.3.0 or later, as it implements the new CLI system. { See PHP documentation }
Before running the command, it’s important to remember that on Windows you don’t need the word php defined as a shortcut; you can run it directly from the folder. For Linux users this issue doesn’t arise.
C:wampphp>php -v
PHP 5.2.3 (cli) (built: May 31 2007 09:37:22)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
or
$ php -v
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
It’s advisable to confirm you have cli, but if you get cgi don’t worry as it can be worked around. We’ll see how later.
The Basics
To start, it’s important to understand that any PHP script will work correctly on the command line. But it’s also immediately apparent that the output won’t look as nice as when viewed in a browser… ; )
So we need to adapt when programming a script to be run on the command line. Here’s a very simple example:
To run this program, go back to the console:
C:wampphp>php hello.php
Hello Console! :D
or
C:wampphp>php C:path_to_filehello.php
Hello Console! :D
More Advanced
It’s important to understand more concepts. When writing programs for the console, we should include an exit(0); at the end of the program to respect “good practice” rules. Also, paragraphs work much more simply — just a \n at the end of the line, unlike
or .
<?php
for ( $i = 0; $i
Output
C:wampphp>php loop.php
0
1
2
3
4
5
6
7
8
9
When programming for the console, we’ll use two new functions: fgets and fwrite. Anyone familiar with other languages already knows these functions. They take a data stream as an argument.
There are three streams that come by default in PHP, associated with these addresses:
* php://stdin (read) - **STDIN**
* php://stdout (write) - **STDOUT**
* php://stderr (write) - **STDERR**
We could achieve an identical program with this alternative:
<?php
for ( $i = 0; $i
Reading Data
Obviously at some point the user will need to make some choices during the program execution.
We can think of it as dealing with a file, which makes it easier to visualize. The only difference is that we use PHP’s input and output streams. The remaining constant, STDERR, is used to store execution errors in an independent stream; we’ll study this in more detail in another article.