PHP

Posts about the server side scripting language PHP

PHP Logo

The Monty Hall Problem In PHP

24th September 2013 - 12 minutes read time

The Monty Hall problem is a counter intuitive problem in probability mathematics that deals with picking the right prize from a set of three doors. The problem is named after the television celebrity Monty Hall and is loosely based on the USA game show Let's Make a Deal.

This has become a popular problem in programming as it is a good exercise in thinking through a problem to prove what outcome actually occurs. Lots of examples have been posted online so I thought I would sit down and attempt to solve it myself. The problem is most commonly summarised as follows (this example was taken from Rosetta Code):

PHP Logo

Sieve of Eratosthenes In PHP

5th July 2013 - 4 minutes read time

The sieve of Eratosthenes is named after Eratosthenes of Cyrene who was a Greek mathematician who devised a mechanism to find a sequence of prime numbers using a simple algorithm.

Normally, looping through a list of numbers and finding the primes can be an expensive process. The seive of Eratosthenes is one of the most efficient way of working out all of the smaller prime numbers below (below 10 million or so).

The sieve works by looping through a list of consecutive numbers, starting at 2. For each number in the sequence the multiples of that number are marked to be removed from the list of numbers. When finished the numbers that are not marked are prime numbers.

This algorithm is pretty straightforward, but from that it is possible to create a simple PHP function that will generate all the prime numbers up to a given number.

PHP Logo

Using XPath With HTML Files In PHP

14th March 2013 - 10 minutes read time

I recently have started looking into making myself a PHP Zend Certified Engineer and after doing a bit of research I found that the standard PHP string and array functions appear to be a large part of the exam material. So as a starting point (and for future revision) I decided it might be a good idea to create a revision sheet for those functions.

PHP Logo

How I Learned To Stop Using strtotime() And Love PHP DateTime

1st March 2013 - 7 minutes read time

The DateTime classes in PHP have been available since version 5.2, but I have largely ignored them until recently. This was partly due to the fact that I was working in PHP 5.1 environments a lot (don't ask) but mostly because I was just used to using the standard date functions that have always been a part of PHP (well, since version 4). I wanted to explain why I will be using the new DateTime classes more from now on and why you shouldn't be hesitant to use them.

Using a combination of strtotime() and date() can handle most things and is a good method to quickly grab a date.

PHP Logo

Bookmarklet To Run XDebug Profiler

5th February 2013 - 2 minutes read time
XDebug is a great PHP debugging tool, but it also comes with a very useful profiler that can tell you all sorts of information about your PHP application. This includes things like memory footprint and CPU load but will also have detailed information about the entire callstack of the code that was run. To enable the profiler part of XDebug you just need to set up a few rules in your xdebug.ini file.
PHP Logo

Xdebug Debugging On A Remote Server

20th January 2013 - 3 minutes read time

I have started to use virtual machines to develop sites rather than installing a local web server. This allows me to replicate the exact setup of the server I will be deploying to with ease. For each virtual machine I set up a shared folder which allows me to store the files locally whilst being able to run the code on the virtual machine. One thing I missed was the ability to use xdebug to debug the sites through Netbeans, so I set about trying to set up the virtual hosts to allow me to use xdebug remotely.

All that is needed was to add a xdebug.remote_connect_back clause to the xdebug.ini file found in the PHP configuration. Set this value to 1 to automatically connect back to any xdebug session that is created on the server.

PHP Logo

Automating Headless Selenium PHPUnit Tests

15th January 2013 - 7 minutes read time

I have talked before about running Selenium tests in PHPUnit but I have only recently come to properly automate things. Getting a Selenium server to start and stop in a script is relatively easy and can be done in a simple script. My original script for running a directory of PHPUnit tests was as follows. I will explain more about how this all works later on in this post.

PHP Logo

Storing Automated YSlow Tests With Show Slow

3rd December 2012 - 8 minutes read time

I was at a meeting of the Manchester Web Performance Group the other day where Tom Taylor gave a talk about some of the performance testing tool he uses at Laterooms.com. He used a ruby script to set up some preferences in Firefox which then ran Selenium to open some web pages and test them with YSlow. The results of the YSlow inspection are then sent to a Show Slow server where the results can be graphed over time. I realise that I've just mentioned a whole stack of technologies there, so let me pick out the important ones:

Selenium is a remote control agent for web browsers, although it is most stable in Firefox. I have written about this tool before but it allows us to automate interaction with a website via a series of selenium scripts. These scripts can be exported into different code formats, including PHP.

PHP Logo

Playing With ReactPHP

19th November 2012 - 14 minutes read time

I recently saw an implementation of a Twitter wall that used node.js to run searches on Twitter and post the results on a webpage. I had been wanting to create something using ReactPHP so I thought this was a good opportunity to have a go. ReactPHP, if you haven't heard of it, is an event-driven, non-blocking I/O that is essentially the PHP equivalent of node.js. The major difference is that ReactPHP is written in pure PHP with no extra components, whereas node.js is a collection of different programs, interfaces and languages. As a first attempt I wanted to create something simple so it needed to use simple JavaScript to load in the latest tweets for a given hashtag from a ReactPHP server. I have to warn that this is a simplistic implementation of ReactPHP, but it shows the basics of how to get started.

PHP Logo

Happy Birthday PHP Script

29th October 2012 - 1 minute read time

When posting happy birthday messages to developers over social media I like to write it in code, just to be geeky. The following code will print the happy birthday song using PHP.

<?php
$string = '';
for ($i = 0; $i < 4; $i++) {
    $string .= "Happy birthday ";
    if ($i != 2) {
        $string .= "to you!\n";
    } else {
        $string .= "dear %s!\n";
    }
}
print sprintf($string, 'name');

This can be easily personalised by just changing the second parameter in the sprintf() function.