Category: PHP

Information and howto articles for PHP

PHP is a powerful open source server-sided scripting language that can be used on a wide variety of operating systems and webservers, including Apache on UNIX and IIS on Windows.

PHP contains numerous extensions which enable you to dynamically create images and flash files, parse xml, connect to remote servers using pop, imap, ftp, http and raw sockets, natively access many database servers including PostgreSql, MySQL and Microsoft SQL Server, compress and decompress files, lookup domain names and and much more.

PHP has an excellent manual and online documentation which covers every function for every extension available.


I currently develop PHP based websites on a Mac using MAMP. When testing the Interspire Email Marketer API on a local version of a site posting data through to an https:// URL with cURL, I discovered that the version of cURL on MAMP has some issues with certs and SSL.

Read More »

PHP for loops with multiple statements in each expression

PHP for loops work in the same way as other programming languages and have a useful feature where each expression between the semi-colons can have multiple statements to be executed when separated by a comma. I found this useful myself when I need to have both a zero based and 1 based index in a loop.

Read More »

Google Analytics API PHP Class – Updates May 22nd 2009

I’ve made some updates to my Google Analytics API PHP Class today and posted the updated class and example files. The changes are documented here.

auth property is now public

The auth property which contains the authorization key after you’ve logged in is now a public property. You could therefore now do this:

$api1 = analytics_api();
 $api1->login($email, $password);
 $api2 = analytics_api();
 $api2->auth = $api1->auth;

You could also save the auth key into a database and use it for subsequent requests; once authorized it’s valid for several days and this can cut down initial request time.

data method start and end parameters

I’ve added the ability to specify the values “today”, “yesterday” and “week” as the $start parameter when calling the data method. “week” sets the start and end dates to be the 7 days before today. Leaving them as false defaults to the month before today (i.e. the same behaviour as previously).

filters parameter added to the data method

The $filters parameter has been added to the data method for specifying filters. When a value is passed in for this parameter it adds “&filters=$filters” to the request string. I haven’t used filters yet myself with the exception of a couple of tests when adding this option.

debug parameter added to the data method

If you set debug to true when calling data the url that is used to call the Analytics API is echoed out to standard output which is useful for seeing what’s going on behind the scenes. If run from the CLI will echo it along with a linebreak; otherwise will put it in a <p> tag and end with a newline.

get_summary method added

A new method get_summary was added to get summary information for a profile: number of visits, number of pageviews, average time on site and pages per visit. I’ll post more about this new function tomorrow.

get_summaries method added

Works the same as the above but gets summaries for all profiles you have access to.

sec2hms method added

This function is from and is used to format the seconds returned in the get_summary method into h:m:s format.

Read More »

PHP email validation with filter_var – updated

Just over a year ago I posted how to validate email addresses with PHP using filter_var instead of having to mess around with regular expressions. As pointed out in a comment, chris@example will pass validation; while that is actually a valid email address (the domain part of an email address doesn’t actually have to have dots in it, e.g. localhost) in most real situations we don’t want it to validate so this post adds a regular expression to the test to ensure the part after the @ contains a dot.

Read More »