PHP

PHP Logo

Debugging And Testing PHP With assert()

8th August 2021 - 16 minutes read time

PHP has a built in debugging and testing tool called assertions, which is essentially the assert() function and a few configuration options.

With this feature you can add additional checks to your application whilst you are developing it so that when you deploy to production you can be sure that things will run correctly. It is simple to run and allows you to embed testing code within your production code without having an adverse effect on performance.

I've seen the use of assert() in a couple of open source PHP projects, but had not really dug into how it works or what it does. As it turns out, assert() is actually very useful and can also be found in other languages like C/C++, Python and JavaScript.

PHP Logo

Installing SimpleSAMLphp Using Composer

11th July 2021 - 8 minutes read time

Security Assertion Markup Language (SAML) is a standard that passes authentication credentials between hosts and essentially allows for a single sign on solution to be created. The standard uses XML files that get passed between the authentication system (known as the identity provider or IdP) and the service users want to sign into (known as the service provider or SP). 

SimpleSAMLphp is an open source application that implements SAML mechanisms and allows for the authentication system to be created as well as some administration tasks to be performed. The system is robust and battle tested, having been integral to the open source authentication systems for a number of years.

PHP Logo

Create Checksums Using The Luhn Algorithm In PHP

16th May 2021 - 14 minutes read time

The Luhn algorithm was created by Hans Peter Luhn and is a way of creating a simple checksum for a number. This algorithm, also known as the mod 10 algorithm, is used in a wide variety of applications but is commonly associated with credit card numbers.

If you look at the numbers on the front of your credit card the last digit on the right is the checksum. An algorithm is done on the other numbers and if the checksum is the same then the number is considered valid.

Outside of credit card numbers, the Luhn algorithm can be used to create a checksum on any number that you want to store. It is especially handy when you want to give users a number that they will be hand typing into a computer. The checksum helps spot any errors in typing in the number before that number is processed. The good thing about the Luhn algorithm is that it doesn't matter how long the number is so it will work with any kind of digit sequence.

PHP Logo

Swapping Between Different Composer Versions

10th May 2021 - 2 minutes read time
I had an issue I was trying to debug today and needed a way to swap between different versions of composer. Composer is one of the few things that I install globally so it was locked at a particular version on the machine I was using. There has been some significant changes between version 1 and version 2 and it isn't quite safe to use a composer 2 package on a platform that has composer 1 installed.  As some projects I'm working on haven't been upgraded yet I needed a way to swap between versions whilst this work was being done.
PHP Logo

Creating A Game With PHP Part 4: Side Scrolling Shooter

10th March 2021 - 12 minutes read time

As another step up from the game of snake I created in my last post I decided to try my hand at creating a side scrolling shooter. Just like my other posts, this is an ASCII based game played on the command line.

A side scrolling shooter, if you didn't already know, moves a scene from right to left across the screen with enemies moving with the scene towards the player's ship, which is on the left hand side of the scene. The player can fire bullets towards the enemies so remove them from the scene.

In order to create a side scrolling shooter we need to define a few elements.

PHP Logo

Seeding Random Numbers in PHP

9th March 2021 - 7 minutes read time

Computers are not that great at creating random numbers as the methods they use are deterministic. That is, they start from a number (called a seed) and apply maths to that number to generate pseudorandom numbers. Most random numbers generated by computers programs use a seed provided by the system, which is generally the current time. If you can guess the initial value then you can start to work out what the random sequence of numbers generated is. Most random numbers, are therefore not suitable for encryption.

PHP Logo

PHP: Find An Array Sequence In An Array

6th March 2021 - 3 minutes read time

I was looking for a function that searched an array for another array, and after not finding one I decided to write it. What I was looking for was a function that took a smaller array and searched for that exact sequence of items in a larger array. As it happens, PHP does have a number of array search functions, but they didn't produce the correct result.

The array_search() function does accept an array as the needle to be searched for, but this does a multi-dimensional search instead. I also saw some techniques using array_intersect() or array_diff(), and although these functions were able to find one array inside another I was interested in the sequence. 

The implementation of the function contains a two loops, one for going through the haystack array and one for the needle array. If the number of matches found equals the number of items in the needle array then the function returns true.

Here is the sequence_in_array() function.

PHP Logo

PHP Live Regex

1st March 2021 - 3 minutes read time

When creating regular expressions for PHP I tend to use the awesome tool PHP Live Regex.

This tool has really helped me over the last couple of years to create regular expressions for many of the projects I've been working on. Everything from validation functions to formatters that use regular expressions have had their expressions worked out using this tool. I even use it for testing one off expressions where I just need to find/replace in a file.

PHP Logo

PHP Strict Types

28th February 2021 - 6 minutes read time

In December 2015, PHP 7 introduced scalar type declarations and with it the strict_types flag. This flag can be used to enforce the type of value passed to functions. Although I have seen it in action in the past, I have never actually used it before so I decided to look into it a little to see what it was about.

For some background, types are not strictly enforced in PHP. This means that it is not only possible to swap the type of a variable from one type to another, but it is also possible to swap a variable of one type to another depending on the context being used. Programmers getting started in PHP will spot this when attempting to print out a boolean value as this will be cast to an integer before being printed out. This means that when attempting to print 'true' PHP will instead print out '1'. PHP will automatically coerces values to be one type to another when passing values to functions.

PHP Logo

PHP instanceof Operator Tips

26th January 2021 - 8 minutes read time

The instanceof operator in PHP is great at making sure you are looking at a type of object before acting on it. Whilst it's straightforward to use on its own, using it can lead to some undesirable side effects.

As a basic example, let's create a couple of interfaces and classes to show the instanceof operator in action. Rather than just have some test names I am using object names that you might find in a system. This consists of User and Order that might form part of a commerce system.

interface UserInterface {}
class User implements UserInterface {}

interface OrderInterface {}
class Order implements OrderInterface {}

If we instantiate the User object we can detect if the user is an instance of the class User like this.