URL: U R a Lifesaver!

December 13th, 2010 by nick

There’s only one thing that sucks more than a Dyson Ball, and that’s dealing with complex URLs that have parameter strings so long it feels like the URL equivalent of a run-on-sentence that doesn’t seem to ever stop yet every piece has its own reason for being there. Do you feel my pain? Then read on to find eternal salvation….

What’s a developer to do when there’s no intuitive built-in method for dealing with these things? Enter the URL (a.k.a. MakeYourLifeEasier) class. Before we get into the nitty gritty, why don’t we let it show off a bit with a few examples.

 * Example 1: Inspect a sample URL.
$url = new URL('http://leeroy:jenkins@www.nickawilliams.com:1337/path/to/subdir/example.php?id=3&page=2#some_fragment');
echo 'Protocol: ' . $url->protocol;	// Produces "Protocol: http"
echo 'Host: ' . $url->host;		// Produces "Host: www.sitecrafting.com"
echo 'Port: ' . $url->port;		// Produces "Port: 1337"
echo 'Username: ' . $url->username;	// Produces "Username: leeroy"
echo 'Password: ' . $url->password;	// Produces "Password: jenkins"
echo 'Path: ' . $url->path;		// Produces "Path: /path/to/subdir/example.php"
echo 'Fragment: ' . $url->fragment;	// Produces "Fragment: some_fragment"
foreach($url as $key => $value) {	// Produces:
	echo $key . ' = ' . $value;	//	id = 3
}					//	page = 2
 * Example 2: Adding to the current page's URL.
// An empty URL defaults to the currently executing script's URL.
// Let's assume the same URL from the previous example.
$url = new URL();
// Modify the page number.
$url['page'] = 3;
// Link to the next page.
echo '<a href="' . $url . '">Next Page</a>';
 * Example 3: Copying an existing URL.
$url = new URL();
// Calling URL::copy() makes a clone of the URL object and all its properties.
// Pass in an array of key/value pairs to simultaneously assign new values.
// Link to the previous page.
echo '<a href="' . $url->copy(array('page' => 1)) . '">Previous Page</a>';
// Link to the next page.
echo '<a href="' . $url->copy(array('page' => 3)) . '">Next Page</a>';

As you can see, this class is great for both inspecting and modifying URLs. The class itself takes advantage of some of PHP’s advanced SPL features:

  • The various components of a URL are accessed as properties of the URL class:


  • Query string variables are accessed simply by treating the URL object as an array:


  • You can even loop through it or call count() on it like a real array.
  • Simply use (or cast) the object as a string to get the assembled URL:

    echo ‘URL: ‘ . $url

And now for the moment you’ve all been waiting for, the guts.

Hopefully this will find its way into your life, saving you some hours and possibly a few bottles of aspirin. If you’ve found this class useful or have any suggestions for improvements, feel free to share in the comments!

Download: URL.php (zip)

The FCC and Net Neutrality

January 6th, 2010 by nick

I recently submitted a comment to the FCC regarding their upcoming actions on net neutrality. Just thought I’d share what I wrote. Would love to hear thoughts/opinions/comments if you have any! Am I wrong? Did I forget something? What do you think?

I want to voice my support for any efforts the FCC makes to promote net neutrality. As a software developer working for a web development company, my livelihood depends on a free, open, and unrestricted internet.

I certainly understand the motives of the telecommunications industry and have no problem with their desire to find new sources of revenue. Managing internet traffic differently depending on the data’s contents and destination would prove to be a very effective method of doing so. I believe that it is the right of any individual or entity in our free enterprise economy to make such pursuits.

However, I also believe every individual has the right to acquire unrestricted access to the internet for any legal purpose. In this case, I believe such a personal right supersedes the right of business entities to pursue revenue and profit.

Ultimately, an internet protected from usage restrictions will yield a greater economic benefit, as it would continue to foster innovation and entrepreneurship. This in turn will grow new businesses and opportunities that would far outweigh the benefits realized by telecommunications providers should they be allowed to handle internet traffic as they see fit.

I ask that the FCC please preserve the very thing that makes the internet such a valuable and useful tool for everyone.

Adobe’s BrowserLab Goes Live

November 4th, 2009 by nick

It is an unfortunate fact of internet life that not all browsers are made equal. Pages viewed in Firefox may look completely different in Internet Explorer or Safari. Most developers consider this a very frustrating part of the development process, and often wish there was an easier way to test things out. Enter Adobe’s BrowserLab. Given a publicly accessible URL, BrowserLab will generate full-size images that depict how a given page will appear in various browsers.

The free service Browsershots and the paid service Litmus also attempt to address this need, but Adobe’s offering is the first to bring together both a well-designed interface and a price tag of $0.

One of the most useful features of BrowserLab are the viewing options available. Aside from the standard 1-up view (viewing the page with a single browser) a 2-up view is also available. This will render a page in two browsers of your choice side-by-side and will even lock scrolling to make it easier to compare the two.

The “Onion Skin” view is the third option, which is a unique view that will lay one browser’s view over another. This view comes with a slider that allows you to transition from one browser to the other. If you want everything consistent down to the last pixel, this is going to be your favorite.

While the selection of available browsers is not comprehensive, it does cover the most common use scenarios which is acceptable for most cases. Opera and Google Chrome are the most obvious omissions from the list. While not a deal-breaker for me, it is something to be aware of if compatibility with browsers is important for your project. Another shortcoming is that, being images, you cannot interact with the page being tested. This means that testing for JavaScript and CSS events is not possible.

Adobe’s offering is very compelling, and currently holds the crown, at least in my opinion, for the most effective cost-free option for testing multiple browsers. It’s sleek interface, viewing options, and reasonable selection of browsers make it a snap to ensure your site looks consistent across the board.

Exploring SPL: Part I

April 27th, 2009 by nick

SPL“With the advent of PHP 5.0 came a new collection of built in classes and interfaces known as the Standard PHP Library (SPL). The goal of this library is to “solve standard problems and implements some efficient data access interfaces and classes.” What gives this library an edge over other third-party offerings is its tight integration into the core of the PHP language and data structures. In this series of blog posts, I will attempt to showcase some of the more useful components of this library.

I. An Introduction

Before we start rifling through SPL’s various interfaces and classes, let’s start things of with a basic overview. The SPL consists of a number of classes and interfaces that are designed to enhance PHP’s ability to work with and iterate over data.

Object Iteration

The library really shines in cases where iteration over an object’s data is necessary. Some basic interfaces such as the Iterator and the SeekableIterator provide the basic tools to do so. There are also some classes that implement these interfaces that can provide the needed functionality right out of the box, and also offer some excellent examples of how these interfaces can be used (take a look at the EmptyIterator and RegexIterator classes for instance).

Walks like an array, talks like an array, looks like an object….

Being tightly woven into the PHP core, SPL also provides the ability to provide access to data stored within an object as if the object were an array. How the data is stored within the object is entirely up to the developer, and could range from referencing a single internal array to the execution of complex code.

There is plenty more that SPL can do, and the release of PHP 5.3 and 6 both promis even more functionality. Now that I have you all worked up over SPL, you’ll have to wait for my next post to get into the nitty gritty details. In the meantime, consider browsing the official documentation located here.

Bedrock Framework Launched!

April 1st, 2009 by nick

Bedrock Framework
I just want to give a quick shout out for a pet project of mine that has launched today, called Bedrock Framework. It’s a new PHP framework that came from code that I’ve been using for personal projects over the years. I finally decided to package it up and release it as an open source project.

Check out the official website for more details:

Link: www.bedrockframework.com

Stack Overflow: Liftoff!

September 15th, 2008 by nick

Today marks the first day Stack Overflow opens its doors to the public. In case you’re not in the know, Stack Overflow is essentially a Q&A site geared towards developers of all kinds. It takes a language/technology agnostic approach and simply provides a framework within which the community can ask and answer techincal questions. Think of it as ExpertsExchange.com except free and much more community driven.

What I Like

One thing in particular that really impressed me was the way in which the system is designed to build trust with its users. When you first open an account, your level of access is somewhat limited. You start out only being able to ask questions, rate answers, etc. As your involvement in the community grows and users rate your contributions (questions, answers, comments, etc.), your user score will increase. As your score rises, you begin to build trust and the restrictions on what you can do are lifted. After some time you can even edit submitted questions wiki-style, re-tag questions, and essentially moderate user interactions.

Another handy addition adds an element of “fun” to using the site. Known as badges, users can earn them by meeting certain requirements (think Xbox achievements). These badges are visible to other users and help a user to build credibility in combination with their score. This type of formula has been proven to be addictive (think MMORPGs), as users basically “level up” the more they participate in the community. This creates the motivation to contribute to the site and ensure it continues to grow (rather than ExpertExchange’s paid model, where users are motivated by free membership to what would otherwise be a mothly charge).

What Could Improve

Overall the only areas for improvement that come to mind are the smaller details in how the system works. Some things are not quite intuitive yet, such as finding unanswered questions related to a specific topic (which is possible, but not immediately apparrent).

It would also be nice if the site would somehow encourage the growth of sub-communities. For example, one could borrow from the MMORPG concept of a guild and create user groups. Each group could be associated with a particular technology or language and could make tools available that would help facilitate communication between like-minded developers.


Joel and Jeff are definitely off to a great start with the site and judging from what they have produced so far, I’m optimistic about its future. Having been involved with the private beta, the team maintaining the site has bee fairly responsive and Jeff seems to be very receptive to user input. It will be interesting to see how the site evolves in the coming months.

The New .phar PHP Package

September 4th, 2008 by nick

Today I discovered a very powerful addition to the PHP world. Phar is an archive extension for PHP that allows an entire PHP application to be packaged into a single file. It’s basically PHP’s answer to Java’s .jar archive format. Don’t get excited yet, it gets better . . .

There are a few things that make this particularly handy. For one, it is being integrated into the next major PHP release (5.3) wich means that any standard PHP installation running the latest version will support .phar files right out of the box.

The other advantage is of course in deployment. Using a .phar file you can deploy an entire PHP application by working with just one file, rather than a buch of files and directories. This is where .phar really shines. Imagine deploying a popular web app like WordPress by simply copying a single .phar file to your web server’s root, rather than figuring out how to zip it up, upload it, and somehow unzip it remotely. Piece of cake!

The devlopment side is equally as simple, being able to access files within the .phar file as easily as if it were another directory in the filesystem. The .phar file itself can be included in a script using any of PHP’s standard import constructs (include, include_once, require, require_once). This also means external libraries can be more easily integrated, having just a single file to include in order to utilize a given library. A .phar archive can also be accessed as a stream using the same functions used to read/write other types of streams.

// Referencing a .phar archive or its contents:
include 'userlibrary.phar';
include 'phar://userlibrary.phar/internal/file.php';
// Utilizing the Phar Stream Wrapper:
header('Content-type: image/jpeg');
echo file_get_contents('phar://userlibrary.phar/img/image.jpg');

Certainly there are disadvantages as well, and this is not a one-size-fits-all solution. Using .phar files does result in a performance hit, meaning traffic-intensive sites and heavyweight applications are probably not the best place to use this tool. It also may not be ideal for a web app that is fairly modular, or other situations where source files might be added or removed on a regular basis. It does have its uses though, and for me it will be a welcome addition to the PHP core.

iPhoney: A Handy Pair of iPhone Goggles

August 25th, 2008 by nick


As mobile access to the web increases, it is becoming more and more important to ensure sites are usable on mobile devices. iPhoney attempts to address the iPhone camp with a desktop application that renders web pages within a virtual iPhone.

After playing around with the app, it is certainly not without its flaws. For one, it is not simulating a true iPhone client environment. This means there is still room for the possibility that pages will display differently on an actual device. The UI is also fairly limited, with the iPhone graphic surrounding the browser window being purely aesthetic with no functional purpose or clickable areas. It would also have been nice to be able to simulate different network speeds to see how pages loaded using typical 3G/HSDPA, EDGE, and GSM connnections.

The program does recreate an accurate pixel-for-pixel recreation of the browser window (320×240). It even lets you rotate the virtual phone sideways to view the page in “landscape” mode. It also does an excellent job of recreating the iPhone’s browser UI, which can be extremely useful for developers building sites that complement the iPhone’s UI appearence. Overall it makes aesthetic testing of a site extremely simple and straightforward, though is lacking in other areas like performance and browser testing. In the end it won’t replace the need for a real iPhone to properly test a site, but it is certainly a step in the right direction.

Link: iPhoney Product Page

Coding Fonts

July 10th, 2008 by nick

Having recently discovered the plethora of coding fonts available online, it occurred to me that quite a bit of thought has been put into something I have always seen as rather trivial. The more I read about it, the more I have come to realize that using a font specifically designed for software development can be extremely beneficial.

My first thought was, “what on earth could require so much thought?” As I continued my browsing, the all the pieces started to fall into place. Some of the things coding fonts specifically try to emphasize/improve upon include:

  • Appropriately styled for typically used sizes (~8pt - 10pt)
  • Smaller line-height (means seeing more lines of code at once)
  • More readable symbols (esp. brackets, parenthesis, etc.)
  • Designed to be monospaced from the ground up (full-width/centered characters, etc.)
  • Reasonable tab sizing.

I’m sure there are plenty of others, but these are some of the things I found valuable right off the bat.

If you’re interested in giving some programming fonts a whirl, I highly recommend you start your exploration with Proggy Programming Fonts. I particularly like (and currently use) the one called CodingFontTobi which really makes reading through large blocks of code a breeze. I discovered it wasn’t fully compatible with OS X (tabs would appear as funky symbols), so if you’ve got a Mac I recommend downloading my modified version.

Write .NET Applications in PHP

June 16th, 2008 by nick

PHP in VS 2008That’s right, you heard me! It’s all thanks to a fantastic project called Phalanger, which adds a super-fast execution environment for a very large portion of the PHP scripting language. Certainly this was only a matter of time, combining one of the most popular open-source languages of the web with one of the most powerful enterprise-grade frameworks.

One of the coolest things about Phalanger is the way it allows you to decide how to implement PHP syntax. There is almost no barrier to entry, as it is designed to implement PHP the same way it is handled natively, but also allows you to use PHP syntax in an ASP.NET application just as though PHP was simply another option among VB.NET and C#.

I’ll admit I have yet to take a crack at it, but having worked with both .NET and LAMP technologies I can’t help but imagine the possiblities. SiteCrafting in particular could benefit from this tremendously, allowing our PHP devs to easily port PHP-based solutions into some of our existing .NET projects. It also means essentially all of our developers could make contributions to a .NET project if the need arose.

I should warn Phalanger is still in it’s beta phase, and has not yet fully implemented the entire PHP language and common libraries. Though it may not be enterprise-ready yet, I see good things ahead.

Link: Phalanger
Link: Tutorials

« Previous Entries