Simple System Maintenance with PHP-CLI
By Akash Mehta2008-01-21
PHP-CLI 101
We'll do things a little differently to the traditional Hello World examples, as we're assuming you're already familiar with PHP. Pull up your text editor and type out the following (beware of copying, as additional characters may have snuck into the HTML; especially watch the quotes):
<?php
$name = fgets(STDIN);
echo "Hello, ".$name.'! The time is currently ".date("r");
die;
?>
Save this file as clitest.php, pull up your command line and cd to the directory in which you saved the file. Now comes the tricky bit.
I'm presuming you made sure PHP was in your PATH, and is therefore available by calling php, instead of typing out the absolute path to the PHP binary. To check, try this:
scyth@wc:~$ php –v
PHP 5.2.1 (cli) (built: Nov 28 2007 23:14:55)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
If you get a similar result, you're all ready for PHP CLI. If your shell reports that it can't find PHP, you may need to type out the absolute path – e.g. /usr/bin/php, /usr/local/bin/php, C:\php\php.exe (Windows) etc. The "Quick start' section above should help you get PHP-CLI up and running if you haven't got PHP yet. Now, we call the script we just wrote:
scyth@wc:~$ php clitest.php
When you run this, you should get a blank screen. No, your script hasn't crashed, nor is it stuck on some highly complex floating point calculations relating to pi. Let's take a look back at our script:
$name = fgets(STDIN);
This line tells PHP to read input from the standard input stream and put it in the variable $name. The script is simply waiting for you to type your name in. A more effective script would incorporate a "Please enter your name: " prompt at the start; here we're just experimenting. So, head back to your console and enter your name, followed by the Enter/Return key.
scyth@wc:~$ php clitest.php
Akash
Hello, Akash
! The time is currently Wed, 16 Jan 2008 17:13:59 +1100draicone@wcultsrv:~$
The wrapping may confuse this a little, but there are three things to note here:
- Our name was output back to us.
- There was a character after our name recognised as a newline.
- There was a missing newline after our script output where the shell wrote its next prompt.
The first of these is good; the last two we'll have to take care of. As we hit enter after we entered our name, PHP-CLI took this as part of the input. A simple call to trim() will solve this. We can also solve the missing newline problem by outputting a \n (or your system's equivalent) at the end of our script. So, back to our code:
<?php
$name = fgets(STDIN);
echo "Hello, ".trim($name).'! The time is currently ".date("r")."\n";
die;
?>
scyth@wc:~$ php clitest.php
Akash
Hello, Akash! The time is currently Wed, 16 Jan 2008 17:21:47 +1100
scyth@wc:~$
So, just as we're used to, we can develop perfectly normal PHP scripts, and bring their power to the command line.
A little more professional...
Let's experiment with removing the 'php', as this is the ugly reminder we're still on an interpreted web-intended scripting language. (Windows users are probably better of skipping over this section, although it's an interesting read.) Any Unix system can achieve the clean call syntax you might have seen in Perl or Python with the shebang. First find out where your PHP binary is: calling which php at console should reveal this; mine's /usr/bin/php. Then add the shebang (also known as the hash bang) line at the start of your script. The shebang line starts with a #!, followed by the absolute path to the binary that should interpret the rest of the file. In our case, this should look similar to #!/usr/bin/php. Now when we call the file we no longer have to specify the php part at the start. You can even remove the .php file extension! Finally, jump back to your console window and run ./clitest (or ./clitest.php if you didn't remove the extension). Here's my final result:
scyth@wc:~$ cat cli2
#!/usr/bin/php
$name = fgets(STDIN);
echo 'Hello, '.trim($name).'! The time is currently '.date('r')."\n";
die;
scyth@wc:~$ ./cli2
Akash
Hello, Akash! The time is currently Wed, 16 Jan 2008 21:05:36 +1100
The shebang tells the system that the remainder of the file should be sent to the interpreter program defined in the shebang line. One of the lesser known facts about PHP CLI is that you can actually call the PHP binary and write out your PHP code, then execute it on demand (typically by entering Ctrl+D). It's a great way to experiment, and functions just like PHP-CLI – in fact, it probably is PHP-CLI, depending on your configuration. The shebang works just like this – it takes your code and sends it through your PHP binary to be interpreted on the fly.
A note – make sure you've got execute permissions on the file. A sudo chmod +x mycliscript should do the trick.
Tutorial Pages:
» Simple System Maintenance with PHP-CLI
» Fundamentals
» PHP-CLI 101
» Get Maintaining!
» Sample Scripts
» Have Fun!
