TweetFollow Us on Twitter

Mac in The Shell: Scripting with PHP

Volume Number: 24 (2008)
Issue Number: 03
Column Tag: Mac in The Shell:

Mac in The Shell: Scripting with PHP

Forget that web stuff, PHP is a great scripting language!

by Edward Marczak

Introduction

Since the inception of this column, I've covered general shell tools, bash commands and general bash scripting. Life in the shell involves much more! There are many other scripting environments - and shells, for that matter! While I won't be stepping totally outside of bash, I will be looking at other ways to automate your environment. While I like bash, I don't necessarily love it. PHP - yes, the one you may know from web development - has a CLI component that makes a great scripting language. Of course, it's installed by default under OS X (version 5.2.4 as of OS 10.5.1). So let's dig in and learn some new techniques.

Why, oh Why?

Why am I forsaking bash? While bash is powerful, it falls down in some key areas, one of which being easy database access. Yes, using the tools we've talked about over the months, we could use the mysql binary, send output to standard out, grep-sed-and-awk our way to get what we're after. But that's not easy. Or elegant.

PHP is a dynamically and loosely typed language with extensions that allow easy access to data outside of its world. PHP supports access to raw network sockets, curl libraries allow access to URLs (ftp, http, https, etc.), and various database libraries allow access to various databases. This support needs to be compiled in, and Apple has made sure we have the tools we need. (You can look at everything that's compiled in by running php -r 'phpinfo();' | less. Once you're looking at that output, search for 'curl' and 'mysql').

PHP is also Open Source, like many of the packages that Apple includes with OS X. It was written by Rasmus Lerdorf in 1995 (as "PHP/FI" which evolved into the PHP we use today), and stands for "PHP: Hypertext Preprocessor". While its goal was for web development, it's also now nicely suited for scripting. (There wasn't always a CLI component).

What can PHP do? Just about anything! Well, anything that you'd use a scripting language for. PHP runs on just about every platform, and there is even a way to tie in a GUI (but that's beyond what I'm going to go into here. See http://gtk.php.net/ for more information).

Do the PHP

Nothing like an example to get things going. Most text editors recognize the .php filename extension and change modes accordingly. TextWrangler, BBEdit, vi and TextMate all have colorizations and in some cases, extra support for the PHP language and its constructs. So, fire up your favorite one, and type the venerable "hello world" program:

Listing 1: hello_world.php

#!/usr/bin/php
<?php
print "Hello, world!\n";
?>

As with all programs that we're going to run from the shell, we need to mark it executable if we intend to run it by name alone (chmod 770 hello_world.php). Notice that I used the shebang line here (line 1). Technically, you could omit that, and run the program like this:

php hello_world.php

However, I think the shebang line along with marking it executable underscores that we can treat PHP like a 'real' scripting language. This way, we can simply:

./hello_world.php

and be on our way. Let's look closer at listing 1.

First, we have the shebang line as discussed. Next, we have this odd-looking construct: "<?php". This simply signifies that what follows will be PHP code. As originally intended, PHP can be embedded in HTML documents. The PHP opening ("<?php") and closing ("?>") tags allow you to jump in and out of "PHP mode." (As an aside, if you're not in "PHP mode," you're in HTML mode, and the php engine will happily spit stray lines back to stdout. That's why there is no blank line in listing 1 between the shebang and the php opening tag. Also, the final closing tag, technically, is optional).

The next line contains a print statement, which simply outputs its arguments to standard out. In this case, the argument is the string "Hello, world!\n". The "\n" character is a newline, which print does not output on its own after printing.

Finally, we close with an ending php tag: "?>". All in all, you probably figured out this short program without explanation. One thing to note, though: unlike the bash constructs that we've seen, PHP expects each line to end with a semi-colon (";"). PHP uses this as a line separator, and the PHP engine will print errors if you omit it at the end of any line or between any separate statements.

Beyond The Beginning

Like any good programming environment, PHP supports comments, variables and all basic constructs such as flow control statements and loops. Let's get the easy one out of the way: good code is commented code.

PHP honors C, C++ and Perl style comments. Let's look at all three in action.

Listing 2: Comment example

<?php
/* example1.php
This short code snippet illustrates good code commenting.
Ed Marczak, 2008
*/
$numargs = func_num_args();      // This retrieves the number of args passed
echo "Number of arguments: $numargs\n";
# Here's the closing tag:
?>

I'm sure I don't need to belabor that any further.

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. A valid variable name starts with a letter or underscore (not a number) and is then followed by any number of letters, numbers or underscores. (With one exception: "$this" is a reserved variable name and cannot be overridden). As mentioned, PHP is both loosely and dynamically typed. This is a fancy way of saying variables take on the properties of the contents assigned to them, and generally 'do the right thing.' As always, an example:

Listing 3: PHP Variable Demo

<?php
$a = "apple";   // a is a string
if ($a=="orange") print "Orange\n";
$a=5      // a is now an integer
?>

(Note: the final closing tag can also act as a semi-colon, hence its omission in listing 3). In general, this aids the rapid development that can be done with PHP. However, if you ever do need to find out what type a variable is at the moment, you have several options. You can print it out using getype() or you can test for it with istype(). You can also use "===" in comparisons to ensure not only that variable contents are the same, but that types match also. You can force a variable to be a certain type by using settype(), or by casting the variable if you're familiar with that from other languages. I won't delve into this much deeper except to say to be careful with this. Again, the PHP engine will try to 'do the right thing.' Take listing 4 as your warning.

Listing 4: Casting example

<?php
$x=TRUE;   // x is a boolean
$x=(string)$x;   // x now contains "1"
$x="9bar";   // string again
settype($x, integer);   // x is now an int, and 0
?>

In a final show of the PHP engine trying to do the right thing, it will "juggle types" as needed. So, if $a is an integer and $b is a float, adding them together evaluates everything as a float, and returns a float. Strings also gain an implicit conversion if used with mathematical operators. "15" + 5 equals integer 20.

Speaking of types, PHP supports the following types:

  • Integer

  • Boolean

  • Float (aka "double" or "real")

  • String

  • Binary

  • Array

  • Object

We'll be exploring these types as we go.

Second Gear

Other things to note about how PHP deals with string variables and quoting. String literals can be specified as single quoted, double quoted or use heredoc syntax. I'll concentrate on the first two for now.

With single quotes, variables are not expanded, and only a backslash need be escaped. You can nest double quotes within single quotes. For example:

print 'Monty Python\'s Flying Circus.';
print 'I like $dollars';
print 'I\'m splitting
      this line';

The first line prints "Monty Python's Flying Circus." The second literally prints "I like $dollars." - without trying to evaluate "$dollars" as a variable. The third example shows that we can even embed the newline character into a single-quoted string.

If a string is enclosed in double quotes, PHP expands variables and interprets certain escape sequences. The major ones are:

  • \n linefeed (0x0A, or 10 in ASCII)

  • \r Carriage return (0x0D, or 13 in ASCII)

  • \t Horizontal tab (HT or 0x09 (9) in ASCII)

  • \v Vertical tab

  • \f Form feed (since PHP 5.2.5)

  • \\ Backslash

  • \$ Dollar sign

  • \" Double quote

In action:

$num_dogs=6;
print "There are $num_dogs dogs\n";

This prints "There are 6 dogs" followed by a newline character.

Flow Control

All flow control deals with comparison for purposes of directing flow or knowing how many times to loop. PHP understands the following flow control comparison operators:

  • == Equal

  • === Identical

  • != Not equal

  • <> Not equal

  • !== Not identical

  • < Less than

  • > Greater than

  • <= Less than or equal to

  • >= Greater than or equal to

Let's use these in a simple example:

Listing 5: Basic Flow Control

if ($a > $b) {
   print "a is greater than b\n";
   $top = $a;
} else {
   print "b is greater than a";
   $top = $b;
}

PHP uses curly brace syntax to create a statement group. Listing 5 illustrates an if flow control structure. Generically, if tests an expression - any expression. If a is greater than b, the flow follows into the first group if statements. Otherwise, we run the statements in the else group.

Control structures can also be nested.

Listing 6: Nested control structures

$i=100;
while ($i<=500) {
   if (fmod($i,2)==0)
      print "$i\n";
   $i++;
}

You may note from listing 6 that if a control structure only has a single statement, curly braces can be omitted (though I recommend always retaining the braces).

Shell Interaction

There are a few ways that you can have your PHP script interact with the shell in general. First, you can have PHP execute other shell commands. Second, you can pass arguments in to the script from the command line. Third, PHP is fully capable of reading from standard in and directing output to standard out and standard error.

The first one is pretty easy: to run a shell command - one that may have no built-in PHP equivalent - you simply use the backtick operator. The output of the shell command is assigned to the variable of your choice. Listing 7 shows this in action.

Listing 7: diskmon.php

#!/usr/bin/php
<?php
$freespace=trim(`df / | tail -1 | awk '{print $5}' | cut -d "%" -f1`);
if ($freespace > 80) {
   print "System volume is at ${freespace}% full - you may want to look at that.\n";
   die();
}
if ($freespace > 50) {
   print "System volume is at ${freespace}% full - seems OK.\n";
   die();
}
if ($freespace >= 0) {
   print "System volume is only ${freespace}% full - no problems.\n";
   die();
}
print "System volume is an indeterminate amount full.\n";

Accepting and handling command line arguments is also a straight-forward venture. PHP populates the variables $argc and $argv (an array) with the count of arguments and the contents of the arguments respectively. A simple example:

if ($argc < 3) {   // we need 2 actual arguments
   print "Usage: $argv[0] param1 param2";
   die();
}
print "You entered $argv[1] and $argv[2].\n";

The first element of $argv (which starts counting from zero) will contain the name of the program being run, as called from the command line. So, if someone symlinks to your program and it is called that way, $argv[0] will contain the name of the symlink.

Finally, PHP can easily handle something like this:

$ codeprep | php > accounting.csv

...where the codeprep application is actually outputting php code. Naturally, PHP will read standard in like so:

$ ls -l | list_filter.php

The program list_filter.php would contain a loop like this:

while ($line = trim(fgets(STDIN))) {
   // Process input here
   print "$line\n";
}

Standard out is standard out: all echo and print statements are sent there automatically. But what if you want to 'do the right thing' and send error output via standard error? Easy: just use fwrite to direct output to that stream:

fwrite (STDERR, "Record number $rec_no is malformed\n");

With this in your code, you can still do this:

$ data_gen.php > datafile.csv
Record number 70 is malformed
Record number 103 is malformed

You still end up with a good data file, but also can be alerted to exceptions.

Conclusion

PHP is just one of the many nice ways to get into, or continue scripting under OS X. The brilliant thing is that OS X treats all scripting languages pretty equally. Additionally, if you're already familiar with PHP from web development, it makes a nice and easy transition into scripting for the system environment. PHP-based scripts can be used for anything that bash or perl scripts are: triggered automation from cron, GUI interaction and even our beloved login hooks.

Next month, we'll dip further into PHP, interaction with MySQL and other PHP-based script topics.

Media of the month: The Illustrated World's Religions: A Guide to Our Wisdom Traditions by Huston Smith. This one has been around for a bit, but its easy reading overviews and beautiful photography make this a good general read and nice reference guide. Once you see the different perspectives in this book, perhaps we end the emacs / vi wars!

Until next month, keep scripting.


Ed Marczak is the Executive Editor for MacTech Magazine, and has been lucky enough to have ridden the computing and technology wave from early on. From teletype computing to MVS to Netware to modern OS X, his interest was piqued. He has also been fortunate enough to come into contact with some of the best minds in the business. Ed spends his non-compute time with his wife and two daughters.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.