Working with numbers

In this chapter, we will work with some numbers. As mentioned in a previous chapter, PHP have two different data types related to numbers: Integers and floats. Integers are whole numbers, without a decimal separator, while floats always carry a fractional part as well. In most cases, integers will be sufficient, and they are faster and simpler to work with.

Let's try some simple calculations, to show you how easy it is to do math with PHP. Here are a couple of examples.
$number1 = 6;
$number2 = 2;

echo "Addition: " . ($number1 + $number2);
echo "<br /><br />";
echo "Substraction: " . ($number1 - $number2);
echo "<br /><br />";
echo "Multiplication: " . ($number1 * $number2);
echo "<br /><br />";
echo "Division: " . ($number1 / $number2);
echo "<br /><br />";
Now that's simple! Basic math with PHP, and it looks kinda like using a simple calculator. But what about numbers which comes from the outside world, e.g. from a submitted form? We will dig into in a later chapter, and one of the techniques we will be using, is checking if a variable holds a value that we can use for math. The is_numberic() function will help us with this. It checks whether a variable is an integer, or if it can be converted to one. For instance, "42" can be converted to 42, but "Hello World", well, that's a tough one. is_numberic will return true in the first case and false in the second case. Here is an example:
$number1 = "10";
$number2 = 20;

if((is_numeric($number1)) && (is_numeric($number2)))
    echo "Result: " . ($number1 + $number2);
    echo "Both variables have to be numbers!";
Now, try changing "10" to a word or a sentence, e.g. "Hello world". You will see that the condition fails, and our warning will be printed. This comes in handy when validating user input, as we will see later on.

In some situations, you may need a real integer instead of a string that is numeric, like in the above example. Even though PHP is loosely typed, type casting does exist - you may cast one type to another. Have a look at this example:
$var = "30";
echo "The current datatype of var is: " . gettype($var) . "<br /><br />";
$var = (int)$var;
echo "The current datatype of var is: " . gettype($var);
We use the int keyword, inside a set of parentheses, to define a new type for the variable. Obviously, it works the other way around as well - you may cast an integer to become a string and so on.


As mentioned, there are two different data types for working with numbers: Integers and floats. In the next example, I will try to show you the difference. Try running this piece of sample code:
$number1 = 10;
$number2 = 3;
$number3 = 5;

$result = $number1 / $number2;

echo "Result as a float: " . $result . "<br /><br />";
echo "Result as a float, rounded: " . round($result, 2) . "<br /><br />";
echo "Result as an integer: " . (int)$result . "<br /><br />";
echo "Multiplication as a float: " . ($result * $number3) . "<br /><br />";
echo "Multiplication as an integer: " . ((int)$result * $number3) . "<br /><br />";
Hopefully the output of this code will describe it self. As you can see, there is a difference between operating with integers and floats. Be aware that PHP will return a float instead of an integer, when dividing two integers. If you cast the result to an integer, it will be automatically rounded downwards.
^ Back to Top