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

Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
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 below... | Read more »

Price Scanner via MacPrices.net

Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more
Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
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

Jobs Board

Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
IT Systems Engineer ( *Apple* Platforms) - S...
IT Systems Engineer ( Apple Platforms) at SpaceX Hawthorne, CA SpaceX was founded under the belief that a future where humanity is out exploring the stars is Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.