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

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.