Tag Archives: PHP

PHP Error Suppression Symbol

Have you ever seen a PHP script where a line is prefaced with a “@” symbol? Ever wonder what it was for? The at symbol in PHP is the error suppression operator. Any expression with you use it with will never throw an error, even if it fails entirely. It works with any expression (function calls, variables, etc) but does not work for things like if/else statements or function definitions.

It’s often used with the mail() function when you want to send a notification email, but don’t care if it goes through or not, so long as the script doesn’t fail catastrophically.

@mail($to, $subject, $message, $headers);

It could also be used in a pinch to include() a file only if it actually exists at a certain path, though you would be better served using file_exists().

CodeIgniter 2.0 Released: No More PHP4 Support!

EllisLab just released version 2.0.0 of the CodeIgniter PHP framework. There are a few interesting new additions, such as a cache driver with APC and memcache support, the option to let controller handle 404 errors, and the deprecation and removal of Scaffolding.

Perhaps the best news is that PHP4 support is being dropped in favor of a minimum version of PHP 5.1. Hopefully this means we will see CodeIgniter move towards a more object-oriented approach, like its spinoff/fork Kohana.

Also, CodeIgniter is being split into two branches: Core and Reactor. Core will be a slowly-updated branch that EllisLab will use for their products (e.g. ExpressionEngine), freeing up the primary community-driven Reactor branch to be more quickly developed.

CodeIgniter 2.0.0 Released [CodeIgniter]

Cache Data with the WordPress Transients API

WP Engineer had an interesting post recently about a WordPress “Transients API” that is used for caching bits of data temporarily. I often use the Options API to cache things from external servers, such as Twitter statuses, so I don’t hit Twitter’s servers more often than necessary (which would slow down page loads). The Transients API is similar, but with the addition of an expiration field. This makes it a much easier solution, even without its other added benefit.

Also of note is that Transients are inherently sped up by caching plugins, where normal options are not. A memcached plugin, for example, would make WordPress store transient values in fast memory instead of in the database. For this reason, transients should be used to store any data that is expected to expire, or which can expire at any time.

It’s simple enough to use the API. You just call the set_transient(), get_transient() and delete_transient() functions where appropriate. You can read up on the usage over at WP Engineer or the WordPress Codex.

Generate QR Codes On-the-Fly With the Google Chart API

You’ve probably seen a QR code before, even if you didn’t know what it was at the time. It’s a little square matrix barcode that can be read by either a specialized scanner or a cellphone with the right software. UPS puts QR codes on their packages for tracking purposes, and many Android phones come with QR readers preinstalled for easily sharing links to apps.

A QR code can contain any sort of textual information, which will be decoded by a QR reader. A web address, a simple message, a phone number, etc.. A good QR reader should figure out what the decrypted data is and act upon it accordingly. If it’s a web address, it will display the web page. If it’s a phone number, it should display the number and offer to call it.

Users of the iPhone or fourth-generation iPod Touch can use a free app like QR Reader to scan QR codes.

What can you use a QR code for? There are plenty of possible applications. Magazines could print QR codes that let you quickly jump to a web page. (Anyone remember the CueCat?) Advertisements could have QR codes that offer more information. You could put a QR on your business card. Want to move a web page you’re reading from your computer to your phone? Create a quick QR matrix and scan it right off the screen!

Now for the fun part… How do you make your own QR codes?

Continue reading →

Parsing Lua Scripts With PHP

Lua is a lightweight scripting language designed to be embedded in larger programs in order to allow for user customization. The most popular application to include a Lua interpreter is probably World of Warcraft. The entire user interface is customizable through Lua scripting, and a sizable community of plugin developers has grown around it.

It’s possible to include a Lua interpreter in a program built with pretty much any imaginable language…including PHP. There is a convenient PHP class, called phplua, that will allow you to do this.

phplua is a PHP extension that enables you to embed the LUA interpreter in a PHP application. Huh? A script language embedded in a script language? This is probably what you are asking yourself now. But imagine you have an application and want to allow users to customize it using some API. PHP will be sufficient if you are the webmaster and want to extend a foreign application by a separate module. But you certainly do NOT want to give an ordinary web user of this application the ability to inject their own PHP code. PHP is simply not designed to do this. At least not in a secure way.

If you have a complex web application that could do with a little bit of user customization, it’s definitely possible to bake in a Lua API. The question is: if you did that, would it make Lua a meta-scripting language?

Building iTunes Links

Have you ever wanted to link to an iTunes page? It’s easy enough to copy a long, nasty-looking URL like http://itunes.apple.com/us/album/yours-truly/id310905907 right from the application by right-clicking on the album art or title. This works well enough if you’re manually linking to an album or iPhone app from a blog post, but what if you need something a bit more user friendly?

It’s not documented terribly well, but you can create much nicer short “search” links using the itunes.com domain. Here are a few examples:

You can even combine the search links with your iTunes affiliate ID, if you know how to do it right. It looks something like this:


Now for the fun part… Wouldn’t it be neat to be able to generate the links automatically? I’m doing that over at Folk Music Site (a fun project I put together last summer, as I thought it would be neat to have a directory of noteworthy folk musicians). Here is some simple PHP that will generate iTunes links on-the-fly:

function get_itunes_link($artist, $album) {
 $affiliate_id = ''; //Linkshare affiliate ID for iTunes
 $artist = clean_itunes_string($artist);
 $album = clean_itunes_string($album);
 $link = "http://itunes.com/{$artist}/{$album}?partnerId=30&siteID={$affiliate_id}";
 return $link;

function clean_itunes_string($string) {
 $string = strtolower(str_replace(' ', '', $string)); //remove whitespace and makes everything lower-case
 $string = str_replace('&', 'and', $string); //Replace ampersands with the word 'and'
 $remove = array('!', '¡', '"', '#', '$', '%', '\'', '(', ')', '*', '+', ',', '\\', '-', '.', '/', ':', ';', '<', '=', '>', '¿', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '©', '®', '™');
 $string = str_replace($remove, '', $string); //Remove special characters that iTunes doesn't like
 return $string;

echo get_itunes_link('Feist', 'The Reminder');

Note: iTunes links should not have accented characters like á or ü. They should be replaced with their base ASCII counterparts (“a” and “u” respectively). The above code does nothing to sanitize these characters, so you might run into problems unless you write some additional logic to handle that. If you’re using a framework like Kohana, you might have a convenient helper function like utf8::transliterate_to_ascii().

PHP 5.3.3 Adds PHP-FPM

If you run alternative server software like NGINX or Lighttpd instead of Apache, you know very well about how you need to run PHP as a standalone FastCGI daemon. (This is because there is no equivalent to Apache’s mod_php.) If you have the faintest idea what I’m talking about, you may be interested in something new in PHP 5.3.3.

The latest PHP release includes PHP-FPM, the PHP process manager.

I’ve managed to find one decent guide to compiling and setting it up so far. It looks fairly simple. I might have to give it a try sometime, as my current setup (which doesn’t use PHP-FPM) tends to hang occasionally.

Authenticating Users With Twitter OAuth

If you’ve ever played around with the Twitter API, you’ll know that many functions require authentication with either a username/password combination or OAuth. Soon Twitter will be turning off basic authentication for security reasons, in favor of the more complex OAuth protocol. There are plenty of benefits, for Twitter and for users and for developers, but the transition will be a bit of a pain.

Fortunately, Net.Tuts+ has put together a tutorial explaining how to implement OAuth authentication in a Twitter application.

It’s a more complicated than it should be to implement the authentication dance (but it’s very easy for the end user, thankfully). There is one plus to using one of the Twitter OAuth libraries: it’s easier to handle API requests. Need to get a user timeline? If you’re already authenticated, you can just do this:

$nettuts_timeline = $twitteroauth->get('statuses/user_timeline', array('screen_name' => 'nettuts'));

No worrying about cURL or file_get_contents(), the library’s classes take care of all of the boring stuff for you.

WordPress to Finally Drop PHP4 Support

At long last, the WordPress project will be ending support for PHP 4. WordPress 3.1, to be released in late this year, will be the last version to support the legacy version of PHP.

For WordPress 3.2, due in the first half of 2011, we will be raising the minimum required PHP version to 5.2. Why 5.2? Because that’s what the vast majority of WordPress users are using, and it offers substantial improvements over earlier PHP 5 releases. It is also the minimum PHP version that the Drupal and Joomla projects will be supporting in their next versions, both due out this year.

The PHP developers themselves dropped support for PHP 4 back in 2008 in order to focus on the PHP 5 branch and beyond. PHP 5 was released six whole years ago and has many improvements over PHP 4.

Mark Jaquith in his announcement says that only 11% of WordPress installs are running versions of PHP under 5.2, and that the decision will likely give those few hosting providers the necessary kick.

How to Display a FeedBurner Average

Tired of ever-fluctuating FeedBurner counts being displayed on your blog? Why don’t you just display a weekly average? Cats Who Blog has a tutorial that shows you how you can retrieve the numbers via the FeedBurner API, average them, and display a rounded figure.

Why do the stats fluctuate in the first place? In addition to the occasional FeedBurner flakiness, the service measures the number of feed aggregators that have pinged the feed in a single day. Stats, of both RSS and website page views, tend to be lower on weekends than weekdays, for example.

How to get a more relevant Feedburner count [Cats Who Blog]