Following on from my last post about creating a command line game in PHP we now have a mechanism to listen to keypresses. The next step from here is to create a simple game. After thinking about a game that would fit into the command line I decided that something simple like tic tac toe (also called noughts and crosses) would be a good starting point. The game board is small and the conditions for winning are pretty simple to understand.
I was watching a documentary about old computers on YouTube recently and it showed a video of an early computer game creating using the command line. This wasn't a text based adventure game, but a game creating using text for the graphics running as a program on the command line. This got me thinking that creating something like this should be possible using PHP. If it was possible on a 30 year old computer then surely it's possible to get PHP to do it, right? I thought it might be interesting to create a series of posts showing how to put this together.
will be released on November 26th 2020 and there is lots being written about the upcoming features in this release has now been released! As this is a major version there will be new features as well as breaking changes so it's important to be aware of what is changing. This will make it easier to think about how PHP8 will effect your applications and what actions you will need to take to ensure you can upgrade without incident.
I thought I would go through a few of the main changes to see whats going to be in the next PHP release.
A good first step is to look at how to install PHP8 so that you can check it out for yourself. If you are using Ubuntu then the simplest way to install PHP8 is to use the existing ondrej/php PPA library. This can be installed using the following commands.
When installing composer dependencies those dependencies are downloaded and stored in the 'vendor' directory. There are options available to install this into a different location than the composer.json file, but it's generally found in the same directory. The vendor directory contains quite a bit of code, which is especially the case if your project contains quite a lot of dependencies. More often than not though, this directory will not contain any code that you have actually written. It will contain the necessary third party libraries that allow your code to work correctly.
A common technique when creating graphics or visual representations of data is to map a value between two scales. This is useful when working on a set of values and you need to map them to a different set of values in order to show them on a graph.
The maths involved here is essentially figuring out the relationship between the value (v) in our initial scale (x, y) and multiplying this by the maximum range of the second scale (a, b).
a + (b - a) * ((v - x) / (x - y))
Taking the value of 0.5, which is in the scale 0 to 1. Mapping this to the scale 0 to 100 means we make the following calculations.
0 + (100 − 0) × ((0.5 − 0) ÷ (1 − 0))
This results in the value of 50.
Converting this maths into a function in PHP we get the following.
The PHP splat operator (...) has been available in PHP since version 5.6. When it was introduced I made note of it but have never really used it, so I thought it might be interesting to explore it a little.
Internally, the ellipsis operator in PHP is called T_ELLIPSIS, although I have heard a few different names for the operator in the past. This includes names like:
- Unpacking operator.
- Packing operator.
- Three dots operator
- Spread operator
- Splat operator
Personally, I think the splat is the correct name for this operator, so I'll be using that from now on.
A Data Access Object (DAO) is a way of taking data out of a database or API and present it in a unified way across your application. As a design pattern this has uses in standardising how a particular bit of data is passed around, without having to resort to using arrays to accomplish the same job.
I was doing some work with PHP's PDO library the other day when I noticed that I could fetch data out of a database using the fetchAll() method with the PDO::FETCH_CLASS flag. This flag automatically returns the data in an object. This got me thinking more about how this worked, but I couldn't find many articles talking about this. Hence this post.
Setting Things Up
Let's take a class called SpecialText that will store an ID and some text, along with a __toString() method to allow the printing of those values. We will use this class throughout this post to store data from the database.
I was looking at some malfunctioning code the other day where the price was pulled out of one API service and sent to another API. The problem stemmed from the fact that the value coming out of the first API was as a string and the second API required the price in pence as an integer.
The difference in formats here meant that the number had to be converted from one format to another. During this process it was found that the value was sometimes out by a single pence.
For example, whilst the first API sent over a value of £20.40, the second API received a value of 2039, which is one penny out. This class did have some unit tests, but the tests but had failed to account for this rounding error.
As it turned out, this wasn't the only problem with the class in question, so I thought I would write up a quick PHP:CSI showing the problems and how I solved them.
The other day I was tasked with creating the weekly quiz for my family. I decided it would be good to do a section that would consist of 10 words that were spelled using symbols of the chemical elements. The questions would be presented as a list of element names, like this.
Barium Carbon Potassium Tungsten Argon Darmstadtium
This breaks down to the element symbols in the following way.
Barium = Ba Carbon = C Potassium = K Tungsten = W Argon = Ar Darmstadtium = Ds
Putting that all together we spell out a word.
After a bit of thinking about what words I could use I decided to write a program that would help me. The questions in the quiz did not go down very well, but I thought I would put the code that I used here.
The first thing I did was to set up an array of elements that I could use to compare parts of words with.
Deployer is a PHP based deployment tool that I have been using very successfully for a number of months now. It comes with a number of different recipes to deploy a variety of PHP based applications, including Laravel, Symfony, Yii, and Drupal.
I first found Deployer when looking for a deployment package that suited my needs. Phing had been my tool of choice for a number of years, but it had proved problematic trying to debug the XML syntax when things needed tweaking. As I was deploying PHP applications it made sense to me to use a PHP based system to do the deployment. I looked at tools like Capistrano, but as I wasn't that familiar with Ruby I was worried that I wouldn't be able to debug mistakes or customise the deployment process easily.