PHP

Posts about the server side scripting language PHP

Avoiding If Statement Typos In PHP

30th December 2007 - 2 minutes read time

It is very easy to break a program with a simple typo. Instead of typing == when comparing two values you type = and actually assign a variable. This is an easy way to introduce a bug as you will not always notice it until your program doesn't work. There is an easy way of avoiding this.

By using the following syntax:

if(100 == $score){ }

Instead of the usual format:

It is very easy to break a program with a simple typo. Instead of typing == when comparing two values you type = and actually assign a variable. This is an easy way to introduce a bug as you will not always notice it until your program doesn't work. There is an easy way of avoiding this.

Odd and Even Numbers in PHP

25th December 2007 - 2 minutes read time

To find if a number is odd or even you can use one of two operators.

The modulo operator (% in PHP) can be used to calculate the remainder of the value divided by 2. This gives a value of 0 for even numbers and a value of 1 for odd numbers. This can be used in an if statement as 0 will equate to false and 1 will equate to true.

Rounding And Displaying Numbers In PHP

24th December 2007 - 3 minutes read time

To round a number in PHP you can use one of three functions, these are round(), ceil() and floor(). All of these functions take number as the input and will round the value depending on the function used.

To round to the closest integer use the round() function.

round(4.4);  // returns 4

To round down to the nearest whole number use the floor() function.