PHP

Posts about the server side scripting language PHP

Best Practice With Return Types In PHP

23rd July 2018 - 9 minutes read time

I've been using PHP for a number of years and have seen the same things being done with return values from functions over and over again. I have always thought of this as pretty standard, but the more I think about it the less it makes sense. Looking back over my career I am quite sure that a few serious bugs could have been avoided if I had not mixed return types.

As PHP is a loosely typed language this gives the developers the ability to change the type of value that is returned from a function. This happens quite often within the PHP codebase itself as many built in functions will return false if an error happened.

A common practice in userland code is to return false from a function if something went wrong. This might be because it is encouraged in PHP itself.

Don't Validate And Format In A Single Function

30th June 2018 - 5 minutes read time

I wanted to impart a piece of advice to do with validation and formatting of user input, although I've never seen anyone suggest it. I guess it would come under the single responsibility principle so it might seem obvious to some people. There can be reasons why this might at least seem like a good idea at the time.

Essentially, if you want to validate that something is correct, don't format it at the same time. These two actions should be done in separate functions or even classes. I hope to demonstrate that using a single function validate and format anything is a bad idea. I'll mainly be using PHP to demonstrate this, but the principle should be pretty much the same in any language.

Take the following function called isValid(). This is an arbitrary and simple example but shows validation and formatting in use in a single function.

Color Sorting In PHP: Part 2

23rd June 2018 - 4 minutes read time

Following on from my last post about sorting colors I have been thinking about different ways of sorting colors. I have been looking at interfaces that allow people to select colors and they will quite normally have a band of colors that does look nicely sorted. As it turns out this is perfectly possible to do if the colors are normalised to remove light and dark variations of different colors.

The easiest way to remove different amounts of lightness and darkness from a color is to convert it to the HSV color space. This way we can just set the value (brightness) and saturation (amount of grey) to be 1. This will change the color by simply removing any information that does not pertain to the actual color. For example, a color that is a very light shade of blue will be changed to be simply blue.

Color Sorting In PHP

9th June 2018 - 14 minutes read time

Sorting colors is the sort of thing that you never really think about until you need to do it. Sorting a bunch of items by their color is useful in a number of applications, but the simplest is just to display items to the user in a more controlled manner. As it happens sorting with colors is a much more complex topic than I originally thought and required digging into quite a bit more maths than I expected.

Incidentally, there is a whole world of color maths that I didn't know existed until I started looking into this. It was worth learning about though.

Setting Up

To start with, I created a little Color class so that I could have a standard way of storing a color. This just takes the red, green and blue values for a color and allows a simple way of accessing those values.

Setting Up A Linux And Apache Server For Deployer

17th May 2018 - 7 minutes read time

Deployer is an amazing tool that is used to deploy websites (hence the name). I have looked at other tools, but because Deployer is built and run using PHP, using it to deploy PHP sites makes sense. It also means that I don't have to figure out complex XML documents or learn Ruby just to understand what the deployment is doing.

I have been using Deployer for a little while now to deploy my own site but I have been using the root user to accoumplish the deployments to get around any permissions issues. When I sat down with the developers at work we looked into how to setup the server so that deployments could be run without giving the tool unfettered access to the server. To this end we set out a plan to create a 'deployer' user on our servers that would be the user Deployer uses to deploy sites.

Collecting Information

PHPNW17: A Review

10th October 2017 - 13 minutes read time

The PHPNW 2017 Conference was run over the weekend of the 30th September to 1st October at the Manchester Conference Centre and I went along to participate. PHPNW has now been going for 10 years and it looks like this will be the last (more on that later) but this year was just as good as ever.

PHPNW 2016

6th November 2016 - 10 minutes read time

The 9th PHPNW was held on the 1st and 2nd of October 2016 and I was in attendance with around 400 people. The venue was the Manchester Conference Centre, which is where the event has been for the past few years. It's a nice place with helpful and friendly staff, lots of rooms, great food and even a good bar. After a quick introduction from the organizers Jeremy Coates and Rick Ogden) and a session from the platinum sponsors UKFast we started the conference proper.

PHPNW15: A Review

17th October 2015 - 8 minutes read time

PHPNW is the 8th annual PHPNW Conference, and I think I'm lucky to be one of the few who have attended every year. This was something that Jeremy Coates mentioned out as we sat down the introductory session, but it was also good to see lots of new people attending the conference as well. It was a great conference with a great community feeling.

The keynote this year was by Meri Williams, who talked about Stealing People Lessons from Artificial Intelligence. Meri's career has been in both development and project management and she was able to use lessons learnt during her PhD thesis on artificial intelligence in order to think about how people respond to work. The concept sounds a little un-emotional at face value, but part of the principles of AI is making sure that all agents have their own goals. Meri was a funny and engaging speaker who talked about all kinds of interesting aspects of how people learn and grow.

Compiling And Installing PHP7 On Ubuntu

11th April 2015 - 4 minutes read time

At the LAMP and Beyond III event (run by PHPNW) this weekend we set ourselves the task of giving PHP7 a go. Below is some nodes from that session.

This assumes that you’ve already installed PHP5.6 along with Apache and MySQL. Installing PHP5.6 via apt-get is fine as we just need some of the dependencies to be present.

To get the the code for PHP7 you need to clone from the PHP repo on Github.

git clone [email protected]:php/php-src.git php-src

Go into the php-src directory and run the ./buildconf command, this will generate a configuration file.

./buildconf

Before you run config you’ll need to install some dependencies (there are one or two).

PHPNW14: A Review

7th November 2014 - 15 minutes read time

The annual PHPNW conference, now in it’s 7th year, was held on the 4th and 5th of October in Manchester, again in the Mancester Conference Centre. The conference was attended by over 400 people with over 30 speakers from all over the world who talked about everything from project management to the internals of PHP. I have been volunteering at PHPNW for a few years now, but this year I was able to attend as a delegate with a number of colleagues from Access. It’s been a few weeks since the event, but I haven’t had a chance to publish this review, so here it is.