Category Archives: PHP

Great PHP developer required in Central Birmingham

Update: This position has now been filled.

So… we seem to be in the same position as everyone else and are looking to hire a new, great, PHP developer!

From the spec:

Big Room Internet are looking for a great PHP software engineer to bring depth to the team and spearhead our future development. You will be working across a range of projects, ideally with experience in seeing through a project from start to finish. You will also interact with customers from taking work requests through to working with them to resolve an issue and get new software approved. You must be self motivated and be able to show that you can work on your own initiative.

Ideally you’ll have several years of PHP website experience, but also the knowledge of best practices when working as a team on projects. You will be able to take the lead on technical decisions for projects along with documenting them and proving that they work.

This position is based in our office in central Birmingham.

Obviously, we do a fair amount of our work using Zend Framework :)

If you’re interested or if you know someone who is, please get them to contact me :)

Displaying an RSS feed in WordPress

My wife decided that she wanted to display a list of her latest AudioBoos in the sidebar of her blog. She looked at the AudioBoo JavaScript widget but decided it wasn’t subtle enough and so she enlisted me to solve her problem.

It turns out that AudioBoo has an RSS feed, so a simple plugin was required. I had a quick look on the extension site, but most are now “widgets” which her theme isn’t set up for or didn’t provide an unsigned list. Hence, I whipped up a small extension for her.

It turns out that WordPress ships with MagpieRSS baked in, so the work to get the feed is trivial:

include_once(ABSPATH . WPINC . '/rss.php');
$messages = fetch_rss($url);

The rest of the work is simply formatting the output. The key requirement that she had was that it should provide an unsigned list with title, date and optionally the summary. Along with providing some customisation for her, this is what I came up with:

function akrabat_simple_rss($options = array())
$defaults = array(
'url' => '',
'number_of_items' => 5,
'display_date' => true,
'date_format' => 'd M Y at H:i',
'display_summary' => true,
'number_of_summary_chars' => 100,
'link_on_title' => true,
'link_on_date' => false,
'css_class' => 'akrabat-simple-rss',

extract (array_merge($defaults, $options));

$output = '';
if (!empty($url)) {
include_once(ABSPATH . WPINC . '/rss.php');
$messages = fetch_rss($url);
if(count($messages->items) == 0){
return '';

if($number_of_items > count($messages->items)) {
$number_of_items = count($messages->items);

$output = '

    for($i = 0; $i < $number_of_items; $i++){
    $message = $messages->items[$i];

    $link = $message['link'];
    $title = $message['title'];
    $date = null;
    if (isset($message['published'])) {
    $date = $message['published'];
    if (!$date && isset($message['pubdate'])) {
    $date = $message['pubdate'];
    $summary = null;
    if (isset($message['summary'])) {
    $summary = $message['summary'];
    if (!$summary && isset($message['description'])) {
    $summary = $message['description'];

    $output .= "

  • ";
    $title_string = htmlspecialchars($title_string);
    if ($link_on_title) {
    $output .= ''.$title_string.'';
    $output .= '


    if ($date && $display_date) {
    $dateString = date($date_format, strtotime($date));
    if ($link_on_date) {
    $dateString = ''.$dateString.'';
    $output .= '


    if ($summary && $display_summary) {
    $summary_string = substr($summary, 0, $number_of_summary_chars);
    if (count(summary) > $number_of_summary_chars) {
    $summary_string = substr(summary_string, 0, -3) . '...';
    $summary_string = htmlspecialchars($summary_string);
    $output .= '


    $output .= "

  • ";
    $output .= "


return $output;

Maybe it’s useful to someone else too, and I’ve documented it somewhere!

PHP 5.3 is quicker than PHP 5.2

I know that everyone already knows this, but I happened to find out for myself recently!

I was looking at the way view helpers work in ZF2 and thought it would be more convenient if we altered the syntax a little. A side-effect of the change was that we’d have to use call_user_func_array, which is perceived as slow. I thought I’d whip up a simple test to find out how much slower it would be over a direct method call.

That is, how much slower is this code:
$a = new A();
call_user_func_array(array($a, 'b'), array(1));

than this code:
$a = new A();

Bear in mind that I don’t do formal benchmarks and have no clue on methodology. Paul Jones is your man for proper testing of performance.

With that caveat out of the way this is the code I wrote to test:



class A
function b($a)

define ('ITERATIONS', 10000000);
$start = microtime(true);
$a = new A();
for ($i = 0; $i < ITERATIONS; ++$i) {
$stop = microtime(true);
echo 'Direct method call: ' . ($stop - $start) . ' seconds' . PHP_EOL;

$start = microtime(true);
for ($i = 0; $i < ITERATIONS; ++$i) {
call_user_func_array(array($a, 'b'), array(1));
$stop = microtime(true);
echo 'call_user_func_array call: ' . ($stop - $start) . ' seconds' . PHP_EOL;

This was the simplest possible scenario I could imagine and so would show call_user_func_array in the best possible light.

I ran the test on my laptop (using the command line) and got these results:

Direct method call: 21.155211925507 seconds
call_user_func_array call: 72.147792100906 seconds

i.e. call_user_func_array is around 3.5 times slower over 10 million iterations.

Nobody uses their laptop to serve a website though! Being the curious sort, I thought I’d test the difference of this script between PHP 5.2 and PHP 5.3 in a completely unscientific way as I’m lazy! I happen to have a server with both PHP 5.2 and PHP 5.3. on it, so I used that:

PHP 5.2:

Direct method call: 8.3424661159515 seconds
call_user_func_array call: 26.904649972916 seconds

PHP 5.3:

Direct method call: 3.35181903839 seconds
call_user_func_array call: 11.5989868641 seconds

As you can see, my server is much faster than my laptop :) Also, the relative difference is the same. i.e. PHP 5.3′s call_user_func_array is as slow as it is in PHP 5.2.

I think I’ll move everything to PHP 5.3 for the free performance gain! Of course, The question remains as to whether we should avoid call_user_func_array or not.

I would however recommend testing your own apps and see if it makes any difference and please don’t take this post as anything other than something interesting I found out!

Some notes on SQL Server blobs with sqlsrv

I recently updated my use of SQL Server with Zend_Db_Adapter_Sqlsrv to use UTF-8 throughout. This turned out to be easy enough:

  • Use ntext, nvarchar types in the database
  • add: resources.db.params.driver_options.CharacterSet = "UTF-8" to your application.ini

I subsequently noticed a problem with storing binary data to a varbinary(max) field. The error was:

An error occurred translating string for input param 2 to UCS-2: No mapping for the Unicode character exists in the target multi-byte code page.


The code looked something like this:

$data['filename'] = 'test.gif';
$data["file_contents"] = $binaryData;

Fortunately, my friend Elizabeth Smith, pointed me in the right direction by suggesting I find out about bindings. So I did some research and it turns out that I just need to use an array for the parameter that I pass into the update() or insert() method of Zend_Db_Adapter_Sqlsrv.

It turns out that all you need to do is change the ‘file_contents‘ element of the array to an array that also specifies the data type. The code I ended up with now looks something like this:

$data['filename'] = 'test.gif';
$data["file_contents"] = $binaryData;
if ($adapter == 'Zend_Db_Adapter_Sqlsrv') {
$data["file_contents"] = array($binaryData, SQLSRV_PARAM_IN,

And all is fine.

The list of constants for the Sql Server Driver for PHP is helpfully available on MSDN and the documentation for sqlsrv_query() is worth reading too!

SqlSrv v2 and long field names

A good proportion of the projects my company undertakes end up on Windows servers with IIS and SQL Server and hence we use the SqlSrv PHP extension from Microsoft. We don’t host any of these projects ourselves and leave it up to the client’s IT department. This is the main reason that we use a database abstraction layer, Zend_Db, in our case as we can swap the underlying database out with less hassle.

A couple of weeks ago, I came across a problem when installing our app onto the client’s server.

It didn’t work.

This was a surprise as we have a few Windows Server VMWare installations on which we had tested and they had worked fine. The most obvious differences were that this server was 32bit and that it was using v2 of the SqlSrv extension. As there were a number of differences from our usual fare to the client’s install, so it took me a while to (a) build a install that exhibited the problem and (b) reduce the problem to something simple.

The error I was getting is this:

exception 'Zend_Db_Statement_Sqlsrv_Exception' with message
'[Microsoft][SQL Server Native Client 10.0]String data, right truncation'
in C:\Websites\project1\library\Zend\Db\Statement\Sqlsrv.php:237

Googling failed me completely!

I eventually narrowed down the SQL statement that caused the problem:

SELECT email_user_include_form_details FROM "forms"

That’s not especially complicated :)

At the point, I yelled for help which came in the form of Brian Swan of Microsoft. There’s quite a few helpful MS guys around nowadays if you keep your ear close to the PHP community, which is really handy. Brian helped prove that I wasn’t going mad and that there really was a problem.

We determined the problem to be that with v2 of the sqlsrv driver, you cannot have a fieldname longer than 30 characters.

Brian assured me that the team are aware of this and it will be fixed in a subsequent update of the driver. Also, it only affects the sqlsrv driver and not the pdo_sqlsrv driver. Hence, there are two workarounds:

1. Shorten your fieldnames to 30 characters or less
2. Use pdo_sqlsrv

Smart readers will already be commenting that there isn’t a Zend_Db_Adapter_Pdo_Sqlsrv and they’d be right, but it isn’t hard to write one :)


I cobbled together an adapter that works for me and have put it on github. As it’s not in the Zend_ namespace, we have to use an additional key in the application.ini file so that it is loaded:

resources.db.adapter = "Pdo_SqlSrv"
resources.db.params.adapterNamespace = "Akrabat_Db_Adapter" = localhostSQLEXPRESS
resources.db.params.username = testuser
resources.db.params.password = testpassword
resources.db.params.dbname = testdatabase

The key difference from a standard Zend_Db adapter is the use of the resources.db.params.adapterNamespace key which tells the system the full name of the class to load.

My limited testing shows that this adapter works with v2 of pdo_sqlsrv which solves my problem with fieldnames that are longer than 30 characters!

Unfortunately, I found out about this too late for Zend Framework 1.11, so I’ll have to look at getting it into Zend Framework 2.0.

On Exceptions

I’ve been reading the Proposal for Exceptions in ZF2 and like it. One thing that caught my attention was that it suggests that you can catch an interface. I hadn’t heard of that ability before, so I pulled out my trusty text editor to have a play.

Consider this code:

namespace My;

interface ExceptionInterface {}

class SplExceptionClass extends InvalidArgumentException implements ExceptionInterface {}
class ExceptionClass extends Exception implements ExceptionInterface {}

class A {
static function throwAnSplException()
throw new SplExceptionClass('oops');
static function throwAMyException()
throw new ExceptionClass('oops again');

Within our My namespace, we have two exception classes and an exception interface that both classes implement. We also define a class MyA as a vehicle for throwing the exceptions. This is the basis of how ZF2 exceptions will work without all the actual component implementations :)

Let’s do some testing to look at what happens:

Test 1

try {
catch (ExceptionClass $e) {
echo "Caught \\My\\ExceptionClass\n";

As expected, this works. We catch the MyExceptionClass exception that was thrown.

Test 2

try {
catch (ExceptionClass $e) {
echo "Caught \\My\\ExceptionClass\n";

As expected, we fail to catch MySplExceptionClass exception that was thrown as it is not related to the MyExceptionClass that we are trying to catch.

Test 3

try {
catch (ExceptionInterface $e) {
echo "Caught \\My\\ExceptionInterface\n";

This time we are catching the MyExceptionInterface and it works! This surprised me and is very handy.

We now have the ability with ZF2 to be able to use different exception classes to represent different error types rather than using string comparison and, at the same time, we can have a single catch() for when we don’t need that level of granularity.

MongoDB on OS X with the stock PHP installation

MongoDB was mentioned a few times at tek and I said that I wanted to have a look at.

Travis’ article, MongoDB: A first look, came out a few days ago and piqued my interest further. Then Matthew sent me some source code that requires it. The stage was set for getting MongoDB working on my Mac.


I use homebrew as a package manager for installing open source bits and bobs like couchdb, git, and hg. Installing MongoDB was simply a case of:

brew install mongodb

Once, installed there’s a convenient LaunchAgent plist supplied so that mongodb starts with the computer:

cp /usr/local/Cellar/mongodb/1.4.3-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist

And at this point, MongoDB is installed on your Mac and Travis’ article and the tutorial work!

The Mongo PHP extension

If you’ve been following along here for a while, then you’ll know that I use the stock PHP that comes with Mac OS X. I’ve been very happy with it so far and installed Xdebug was easy enough using pecl, so I was hopeful that the mongo extension would be equally simple.

Turns out that it is!

pecl install mongo

Compiles the extension with no problems.

To add it to your PHP install, edit php.ini and add:

A quick sudo apachectl restart and phpinfo() shows this:

MongoDB in phpinfo

All done! You can now get at MongoDB from your PHP scripts.

Sending a file to IE using SSL

I keep coming across this one, so I’m noting it here so I can find it again.

Internet Explorer doesn’t like certain headers related to caching when you send it a file from an SSL site. The Microsoft knowledge base article, Internet Explorer is unable to open Office documents from an SSL Web site explains the problem quite well:

When you attempt to open or download a Microsoft Office document (.doc file, .xls file, .ppt file, and so on) from a secure Web site in Internet Explorer, you may receive one of the following error messages, even though the document is available and downloaded from the server

It turns out that the problem is directly due to sending these headers:

Pragma: no-cache
Cache-control: no-cache,max-age=0,must-revalidate

So, make sure you don’t!

Incidentally, IE6 also gets upset if you set max-age to 0 and attachment to inline, so don’t do that either!

Redirecting email whilst developing

One common problem whilst developing is that you don’t want to send emails out to the client (or their clients!). Ideally, we want to alter our development environment so that this doesn’t happen, but still allows us to test the contents of emails that are sent by our web applications.


On Windows, the mail() function uses SMTP over port 25. Unless you’ve changed your php.ini file, then it will try to connect to localhost in order to send an email. On Windows VM, I use Fakemail. This is an SMTP mail server written in perl (or python) that store emails as files into a given directory. When your web application sends an email, you check in the directory and look at the files created. One top tip: alter the script to give each file a .txt extension. Then you can double click :)

Fakemail is also very useful in Linux/Mac if you are using the SMTP transport mail in Zend_Mail or Swiftmailer or whatever.

Linux / OS X

On the *nix based systems, mail() sends email using an application on the system called sendmail (or any number of compatible alternatives). By default it will call the sendmail binary, however you can change this in your php.ini with the sendmail_path setting.

I set my development boxes like this:

sendmail_path = /usr/local/bin/trapmail

Now, mail() will call my trapmail script. This script is trivial:

formail -R cc X-original-cc \
-R to X-original-to \
-R bcc X-original-bcc \
-f -A"To:" \
| /usr/sbin/sendmail -t -i

This script causes all emails to be redirect to my email address with the original to, cc and bcc fields renamed in the headers, so they can be checked!. I like this solution even better than Fakemail as it’s easier to see exactly what the mail looks like in a mail client, especially for HTML format.

I just wish I had thought of this script myself! However Sean Coates came up with the idea in 2005 and I’ve been using it ever since.

PHP Advent 2009: On deployment

This year I was asked to write an article for PHP Advent 2009 an it’s now been published!

Automate your Deployment is a look at how to automate the process of deploying your application to the web server. At my company we started automating our deployment systems just over a year and the number of issues we have around deployment of new code to a website has dropped considerably and is no longer a stressful event.

If you aren’t currently using an automated deployment script, I can’t recommend highly enough that you set yourself a New Year’s resolution to investigate the options and implement a system for yourself.