Category: PHP

Xdebug .htaccess settings

You’ll need to add these to get it working.

php_value xdebug.remote_host <your IP.0.0.1>
php_value xdebug.remote_enable 1
php_value xdebug.show_exception_trace 1

FizzBuzz Test

This morning someone on twitter (@brooklynDev) commented about how good the FizzBuzz test was to weed out bad programmers. I’ve been interviewing for a few jobs recently and by co-incidence so I wanted to give it a go.

In essence it is as follows:

"Write a program that prints the numbers from 1 to 100.
But for  multiples of three print “Fizz” instead of the number and for the  multiples of five print “Buzz”.
For numbers which are multiples of both  three and five print “FizzBuzz?”."

Pretty straight forward right? I thought so anyway… Here’s my solution. It took about 5 minu

$i = 1;
while($i <= 100) {  
$divThree = (($i % 3) > 0) ? false : true;
$divFive = (($i % 5) > 0) ? false : true;

if($divThree) echo 'Fizz';
if($divFive) echo 'Buzz';

if(!$divThree && !$divFive) echo $i;

echo '';

$i++;
}

I saw another solition which involved the use of a case statement with a “true” test condition which I also liked, but it is not something which immediately jumped to mind for me.

Retrospective coding

Its interesting that since my last post I’ve been made redundant from my position at The Sound Alliance. They cut back the development team to bare bones as the good work we did made maintenance a lot easier. Effectively we coded ourselves out of a job! I wont go into it here though.

Anyway, I’ve found another position at The Royal College of Physicians and have had a week and a half off. Well not really “off” but working on a freelance project for an old client. It involves implementing some workflow automation within their Intranet that I developed about over the last 4 years and haven’t touched for 2 years .

What I’m amazed by in this is how my code style and even logic evolves over time. Its made me realise that I learned a lot of good technique while at TSA. Its also highlighted some of the great design decisions and techniques I already posessed.

If you look at your old code and cant see room to improve, you’re missing something – or its genuinely good code!

The dynamic nature of PHP defines – you don’t need to declare all variables, inherently adding the notion that adding them initially was a pursuit to be avoided.

So why re-implement a feature people bothered to un-impliment?

$this->that;

VS ~

$this->getThat();
function getThat(){
    return $this->that;
}

At least make redundant set/get [tters] dynamic. Use of the magic __set() and __get() methods means the benefit of protecting access to your objects properties can be encapsulated into your base class easily enough.

PHP __LINE__ incorrect

I had some weirdness with the __LINE__ constant reporting the incorrect line. A colleague helped me get to the bottom of it.

There were too many line endings in the file. The only way to “see” them was in vi. They appeared as ^M characters.

The solution, replace them with nothing. I’m certain there are other ways, but this worked for me.

:%s/^M//g

To get the ^M you need to Ctrl+v then Ctrl+m.

Thanks superspace!