PHP functions and varying numbers of arguments

Added on: Friday 29th May 2009

There are times when you want to pass an unknown number of arguments to a function. A simple example might be a function to add a list of numbers.

At this point I can hear people shouting - 'to do this you just pass a single parameter that is an array of numbers to add'.

In fact this is what I thought I could do for a bit of code that I needed to add to our content management system but it didn't work.

The problem was that I was using the php function call_user_func_array. This function has two parameters, an array containing the class and function to call and a second array with the function parameters.

I mistakenly thought that the call_user_func_array function would pass the second parameter as an array to the nominated function but it doesn't.

If the function being called has one defined parameter then call_user_func_array passes it the first item in the array and ignores the rest.

There is an easy way around this using the func_num_args and func_get_arg php functions.

In the case of adding a list of numbers the code within the function would be:

for ($i = 0; $i < func_num_args();$i++) {
      $return = $return + func_get_arg($i);

Just remember not to specify any parameters in the function definition.

Ordnance survey maps on your website

Added on: Thursday 28th May 2009

I've recently come across an application from the Ordnance Survey that allows you to embed maps on your website much like the very popular Google Maps.

The difference is that the mapping used is from the Ordnance Survey which has much more detail than Google Maps.

In all other respects this looks to be very similar to Google's application - you need an API key which covers a single domain and they quote usage limits of 30,000 map tiles of data and 1,000 place name lookups a day.

The service offers maps covering England, Scotland and Wales and you can place any kind of information that has a geographic reference on top of the Ordnance Survey maps and interact with a map - pan, zoom in and out, add markers and polygons.

It all sounds very exciting. Just need to learn the API!

Expect a post here soon with details of how I get on with it.

For more information see the OS OpenSpace website.

Dartmoor Showcase Day

Added on: Thursday 14th May 2009

I had a great day out yesterday when I was exhibiting at the Dartmoor Partnership Showcase Day.

The event, held at the lovely Two Bridges Hotel, was organised by the Dartmoor Partnership which used to be the Dartmoor Tourist Association.

Membership has now been opened up to all trades and services based on Dartmoor and yesterday more than 50 exhibitors came together to show what they can do.

The was an excellent opening address from Malcolm Bell - chief executive of South West Tourism - and throughout the day there were others talks and demonstrations.

Above all though it was a good chance to network with other local businesses and meet new people.

If you have a business based on Dartmoor and aren't already a member of the partnership I would thoroughly recommend it.

Short URLs

Added on: Saturday 9th May 2009

URL shortening services such as tinyURL have been around for some time now. Originally developed to overcome the problem of links wrapping over several lines in email clients they are now more prevalent because of social media applications such as Twitter

With only 140 characters per 'tweet', if you want to include a link to your website or an article on another site you don't want the URL to take up most of the message.

This is where URL shortening services like tinyURL can be useful. It is no longer alone however. A quick Google search shows there is no shortage of other services.

The top 4 services used on twitter are,, and

Each of these tries to offer something unique over the other. For example a short url on tinyurl is 25 characters long and on it is 20 characters but yet another service offers short urls that are 14 characters long.

So if you need to squeeze an extra few characters into your message then could be for you.

Is this the only reason for choosing a URL shortening service though?

For me there are two other essentials

  1. To be able to track clicks on a URL
  2.  To be able to use an API in order to create the shortened URLs.

Many of the current services do offer both of these features and there is one that I've found - - that even has the statistics available through their API.

Usually the API is very simple - you tack the URL you want to shorten onto a URL for the API and the return value is the shortened URL.

For example for tinyURL the address is:[[url]]

where [[url]] is the URL to shorten. In fact it works both ways - if [[url]] is a tinyURL (ie shortened URL) it returns the original URL.

We've now wrapped several of these APIs into a function call and made a Widget to shorten URLs in our CMS.

Of course it is not difficult to create your own URL shortening if you have access to a database and server side scripting.

  • Create a table in the database to store the original URL and a unique ID for this URL.
  • Add some server side code to take a URL and check whether it already exists in the table - if it does, grab the unique ID - if it doesn't create a new record then grab the unique ID.
  • Replace the original link with a link to a new page (with a short name like link.php) and pass as a parameter the unique ID.
  • This page then looks up the original URL associated with this ID and redirects the browser.
  • As an added bonus the page can also record visits to the page (and hence clicks on the shortened URL).

You can even go further and add a userID field to the table so that each unique ID is now for the user/URL combination meaning that you can track clicks by individuals.

For more information, contact me using the contact page.

Of course unless you've got a very short domain name in the first place you are never going to match any of the above services but you will have more control.