PHP

Posts about the server side scripting language PHP

Colour Sorting In PHP: Part 4

Following on from by previous post about sorting colours I decided to take another step towards sorting colours by segmenting the data to create a further dimension to the multi-dimensional array.

The array is already split into segments based on the hue of the colour, but we can further split this by separating out saturation and value into separate arrays within hue. To do this we set the saturation or value to be a constant and push them into separate arrays.

The new code looks like this. Please excuse the duplication of code here. This is just a simple example to show how the array is put together.

Colour Sorting In PHP: Part 3

The last time I looked at sorting colours I had produced a nice band or sorted colours, but to do so I had essentially removed a third of the data from the colour information. This meant that there was no white or black colours in the band of sorted colours.

After a bit of thinking on how to solve this I hit upon a way of using a two dimensional array of colours to filter the colours into blocks. This would allow the missing colour information to be rendered correctly, and would only mean a small amount of work to allow it to work with the rendering function used in the previous examples.

Generating The Data

I could easily just generate every colour available and use that as the data. What I wanted to generate was a random assortment of colours that would represent the sort of data being produced by a system or other input.

SOLID Principles In PHP

SOLID is a set of object oriented design principles aimed at making code more maintainable and flexible. They were coined by Robert "Uncle Bob" Martin in the year 2000 in his paper Design Principles and Design Patterns. The SOLID principles apply to any object oriented language, but I'm going to concentrate on what they mean in a PHP application in this post.

Best Practice With Return Types In PHP

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

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.

Colour Sorting In PHP: Part 2

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

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

Bogo Sort In PHP

I came across this sorting algorithm the other day called 'bogo sort'. This is a sort of joke sorting algorithm that is highly ineffective at actually sorting arrays. The name comes from the word 'bogus'.

Here is how it works.

  1. Take an array.
  2. Shuffle it.
  3. If the array sorted then stop.
  4. If the array is not sorted then return to step 2.

As you can see, this isn't so much a sorting algorithm, more of a game of chance. Due to the random nature of the sort you might need to wait a very long time before the array is actually sorted.

Here is an implementation of a bogo sort in PHP.

An Implementation Of Array Binary Search In PHP

I have been doing some reading and watching lectures of programming theory recently and I was reminded of this algorithm I learned about in university. Binary searching an array is a divide and conquer algorithm that takes an array and searches for a value in that array by splitting the array into halves. The algorithm works like this.

Colour Sorting In PHP

Sorting colours is the sort of thing that you never really think about until you need to do it. Sorting a bunch of items by their colour 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 colours 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 colour 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 Colour class so that I could have a standard way of storing a colour. This just takes the red, green and blue values for a colour and allows a simple way of accessing those values.

Setting Up A Linux And Apache Server For Deployer

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