Category Archives: Example Code

Validate numbers in PHP without using is_integer() or is_numeric()

One of the great features of PHP is its automatic typecasting of variables. You don’t need to tell PHP whether you are storing a string, a float or an integer value. This gives rise to some interesting error checking scenarios though. One of the simplest, is checking whether the value submitted from a form is numeric or not.

Annoyingly, every value submitted from a form is classed as a string in PHP. That doesn’t stop you performing calculations on the submitted value as if it were a number, but it does mean that is_integer() will always return false for that value. The PHP manual advises us to use is_numeric() instead, however is_numeric() and is_integer() are very different. For example, is_numeric() is quite happy to accept any valid number, which can include minus values, floating points and exponential numbers. For error checking this can be pretty worthless. Let’s look at a couple of different scenarios:

Scenario 1: we are passing a database ID in a hidden INPUT tag, and we want to ensure that only a valid integer ends up in the subsequent database query.

We can actually typecast the variable as follows:

$id = (int)$_POST['id'];
or
$id = intval($_POST['id']);

Nice and simple. However, minus values can also be valid integers, and sometimes we don’t want those either. We could run two tests, but that’s not particularly elegant. So…

Scenario 2: we are passing a monetary value to a payment gateway and it has to be in the lowest currency denominater (e.g. £10.53 becomes 1053). In this scenario, floating points, minus symbols and exponentials are all unacceptable.

The simple answer is regular expressions (regex) using preg_match.


if(!preg_match("/^[0-9]+$/", $_POST['amount']) {
// not valid
} else {
// valid
}

How to run Football Manager 2009 in a window on Mac OS X

I was very frustrated to find that you cannot set Football Manager 2009 to run in a window from the in-game preferences as you could with previous versions. I’ve figured out a quick fix using Terminal (which you will find in your /Applications/Utilities folder). Type (or copy and paste) the following command into Terminal to launch Football Manager 2009 in a window:

/Applications/Sports\ Interactive/Football\ Manager\ 2009/fm.app/Contents/MacOS/fm -windowed -small_screen

There’s a more user friendly way of doing this, which I’ll explain at another time. For now, I’ve got teams to manage…

PHP mail() and ssmtp on Debian Linux

If, like me, you have a dedicated mail server that you want your PHP scripts to use, you may have come across ssmtp. ssmtp is a cut-down MTA that simply sends mail off to another server – ideal for scenarios where you don’t need to receive mail on your web server, but you do want to be able to use the PHP mail() function. Read more »

PHP – include all files in a folder

Ever wondered how to include every file, or a series of files in a folder without having to reference them individually? It’s easily done with PHP’s glob() function. Read more »

Using Recursive Functions in PHP to Manage Hierarchy

The recursive function is an essential tool for anybody developing PHP sites that feature multiple levels of categories, pages etc. In fact, they are great for managing hierarchical relationships (sometimes referred to as parent/child relationships). There are many applications of this, but the example I’m going to show you is categories, such as you might use in an eCommerce site. This is one of those wonderfully simple techniques that once grasped, will open up a myriad of possibilities for the programmer.

Read more »

PHP Random Secure Password Generator Class

UPDATE: If you don’t want to spend the time implementing this code, and just want to generate some passwords, I’ve put it on a separate website: www.random-password.net.

I just recently had to create a function to generate random passwords on a site I was developing and thought it would be an ideal thing to expand upon and put into a class. If you’re not familiar with using classes, I also have integration code below.

This source code is provided free of charge under the condition that my name remains upon it. Any modifications, improvements and suggestions will be gratefully received.

Read more »