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

Fallout Shelter pulls in ten times its u...
When the Fallout TV series was announced I, like I assume many others, assumed it was going to be an utter pile of garbage. Well, as we now know that couldn't be further from the truth. It was a smash hit, and this success has of course given the... | Read more »
Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
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 »

Price Scanner via MacPrices.net

Verizon has Apple AirPods on sale this weeken...
Verizon has Apple AirPods on sale for up to 31% off MSRP on their online store this weekend. Their prices are the lowest price available for AirPods from any Apple retailer. Verizon service is not... Read more
Apple has 15-inch M2 MacBook Airs available s...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs available starting at $1019 and ranging up to $300 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at Apple.... Read more
May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.