PHP

Posts about the server side scripting language PHP

PHP Logo

Generating A PDF From A Web Page Using PHP And Chrome

8th May 2022 - 12 minutes read time

Generating a PDF document from a web page through PHP can be problematic. It's often something that seems quite simple, but actually generating the document can be difficult and time consuming.

There are a number of libraries available that allow you to generate PDF documents, but in reality they solve one problem and introduce several more. Packages exist as either HTML to PDF converters or allow you to generate a PDF by placing elements into the document.

Generating a PDF manually is not normally a good way to go just due to it being a very time consuming process and is difficult to test. You will probably have to spend time creating a rendering engine just for this purpose, and that will take time to create and maintain.

Read the full article

PHP Logo

PHP:CSI - Date Is Less Than One Month Ago

27th February 2022 - 7 minutes read time

Working with logic surrounding dates can sometimes be difficult and it's fairly common to come across really subtle date and time based bugs.

I was recently shown a bug in a PHP application that looks like it should be working at face value, but doesn't actually produce the correct result.

The issue in question was surrounding a date comparison. A collection of objects containing dates were compared in order to find those dates that were less than a month old. This worked fine in testing since all testing dates were less than a month old. After the system was in use for a number of months it became clear that the check wasn't working so further investigation was needed.

Read the full article

PHP Logo

An Introduction To Object Reflection In PHP

30th January 2022 - 24 minutes read time

Reflection is used to gather more information about code during the runtime of the program. This allows code to inspect itself and to make small modifications, which is useful in a variety of situations.

This is possible in PHP thanks to the reflection classes, which are built into core. PHP is capable of using reflection to inspect all types of classes, object, functions, variables and even the recent addition of Fibers. Using the reflection classes we can find out the internal structure of a class that an object is part of, including properties, methods constants as well as what sort of class we are dealing with. Reflection works even if the property of method is private or protected.

This post will concentrate on objects (and their associated classes) as the code used to inspect objects can generally be applied to other forms in PHP.

Read the full article

PHP Logo

Run PHP Through A Browser With PHPSandbox

23rd January 2022 - 5 minutes read time

I use sites like CodePen and JSFiddle all the time to write JavaScript and CSS, but I've not seen a similar site for writing PHP applications. I have used 3v4l.org to test small scripts using different versions of PHP, but it doesn't allow dependencies to be added and doesn't have a nice editing interface.

Recently, I found a site called PHPSandbox that allows users to create PHP applications that can be run in the same way as applications on CodePen. This includes using composer to inject dependencies into the application and a nice little editor interface to see the output and logs from the application.

Read the full article

PHP Logo

Fibers In PHP 8.1

18th December 2021 - 20 minutes read time

PHP 8.1 comes with a few new additions to the language, and one that I have seen get the most attention is fibers. This is a feature that allows PHP code to be executed in a concurrent thread, with the ability to pause and resume that code at will.

Using fibers doesn't mean that the code runs in parallel, rather that the code is executed away from the main thread in a virtual or green thread. These are threads create by and executed by the PHP VM, rather than being executed by the CPU and managed by the underlying OS. This lightweight thread of execution is also called a coroutine and are executed in sequence, rather than being parallel.

Fibers are intended to eliminate the distinction between synchronous and asynchronous functions in PHP and provide a petter mechanism to manage blocking code.

Read the full article

PHP Logo

Debugging And Testing PHP With assert()

8th August 2021 - 18 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.

Read the full article

PHP Logo

Installing SimpleSAMLphp Using Composer

11th July 2021 - 10 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.

Read the full article

PHP Logo

Create Checksums Using The Luhn Algorithm In PHP

16th May 2021 - 16 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.

Read the full article

PHP Logo

Swapping Between Different Composer Versions

10th May 2021 - 3 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.

Read the full article

PHP Logo

Creating A Game With PHP Part 4: Side Scrolling Shooter

10th March 2021 - 14 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.

Read the full article