PHP

Posts about the server side scripting language PHP

PHP Logo

PHPUnit Skeleton Classes

25th July 2011 - 7 minutes read time

If you create classes in PHP then you should be unit testing them as much as you can. Setting up unit testing classes for your code can be time consuming and involve a bunch of copying and pasting. Thankfully, PHPUnit comes with a couple of helper functions that allow the creation of unit testing classes automatically, which can save a bit of copying and pasting.

As an example for this post I will use the following Spider class, which is part of some code I am working on at the moment to create a simple site spider in PHP.

PHP Logo

Netscape HTTP Cooke File Parser In PHP

30th June 2011 - 5 minutes read time

I recently needed to create a function that would read and extract cookies from a Netscape HTTP cookie file. This file is generated by PHP when it runs CURL (with the appropriate options enabled) and can be used in subsequent CURL calls. This file can be read to see what cookies where created after CURL has finished running. As an example, this is the sort of file that might be created during a typical CURL call.

PHP Logo

Number Pair Additions In PHP

16th April 2011 - 4 minutes read time

I was helping my son out with his maths homework today and we came across a question that asked him to find some combinations of two numbers that added up to a given number (he is only 6 so this was good maths practice). This got me to thinking about how to calculate all possible ways in which two numbers could be added together to make another number.

The only rule had to be in there was that doing things like 1+11=12 and 11+1=12 are basically the same thing and so would be cheating. In which case I realised that it would only be necessary to use half of the numbers when creating the list, so for the number 12 every combination can be found using the numbers 0-6 because after this we start duplicating the sums, just swapping the order.

Here is the function I came up with.

PHP Logo

Revisiting filter_var() and FILTER_VALIDATE_URL

1st April 2011 - 6 minutes read time

Quite a while ago I looked at using the filter_var() function to validate URL's using the FILTER_VALIDATE_URL flag and someone pointed out recently that this function has not only changed since the initial release, but that a number of flags can be added to change the way that this function works. Here are the flags available.

PHP Logo

PHP Unconference Europe 2011: Part 2

3rd March 2011 - 27 minutes read time

Sunday

Due to the crappy train service from my home town I had to drive into Manchester on Sunday, but made it in good time to have a coffee before we went to vote on the talks. Some people had a heavy night drinking the night before and therefore didn't make it in the morning (or at all). Some has also opted to only attend the Saturday and so there were a few faces missing, but there were still plenty of people there.

PHP Logo

PHP Unconference Europe 2011: Part 1

27th February 2011 - 20 minutes read time

This weekend saw the PHP Unconference Europe 2011 event in Manchester. The organisers were hoping that the rain would hold off for the weekend, and being true to form Manchester was wet and cold. Being an unconference the talks and discussions are mainly casual and dependent on what everyone decided to see on the day. The event ran across Saturday and Sunday with different talks on each day.

Saturday

Picture taken by Lorna Mitchell.

PHP Logo

PHP Array Of Countries

3rd February 2011 - 12 minutes read time

Use the following array if you want to get a list of countries, along with their codes. These codes are the 2 letter ISO code, the 3 letter UN code and the 3 number UN code. I had to build this the other day in order to present a list of countries in a form so I post it here in case I need something like it again.

PHP Logo

ReCaptcha Not Displaying In IE6

6th January 2011 - 4 minutes read time

I came across a bit of an issue with ReCaptcha and IE6 today, so I though I would write about it in case anyone else had the same issue (there wasn't a lot of stuff on Google about it) and so I can remember what I did in the future.

The form I was using was a multi stage form and the second part contained a call to the ReCaptcha function recaptcha_get_html(), which was part of the PHP library I was using (1.11 in this case). When users with IE6 came onto this page the ReCaptcha box was missing, but after refreshing the page the box appeared. ReCaptcha works by downloading and running a block of JavaScript from Google. After a little research it looked as though IE6 would not download this JavaScript when redirected to the page via a form post (other exceptions might occur) but would if accessed directly or after a refresh. The snippet below if the code that is returned from the Google on request:

PHP Logo

Lazy Coding In PHP; A Mini Rant

4th January 2011 - 5 minutes read time

If there is one thing in the PHP world that really annoys me it's programmers writing what I call "lazy code". This is code that works but takes the least amount of time (generally meaning keystrokes) to create. This is almost always a bad thing as it is difficult to read, hard to change and almost always uncommented. The main problem is that PHP is quite a forgiving and fluid language in that you allows you to write code in a variety of different ways and formats.

The most common problem I have come across with lazy coding is when programmers write if statements. These can be written in a variety of colourful ways, the most common approach I have found is to leave out the curly brackets, like this:

PHP Logo

Converting To And From Decimal Time In PHP

29th November 2010 - 3 minutes read time

To convert a time value into a decimal value representing the number of minutes can be useful for certain calculations. The following function takes a time as a string of hh:mm:ss and returns a decimal value in minutes.