Numeric Data Types
You have already seen that PHP
To check whether a value is either of these types, you use the is_float and is_int functions. Likewise, to check for either numeric data type in one operation, you can use is_numeric . The following example contains a condition that checks whether the value of $number is an integer:
$number = "28";
if (is_int($number)) {
echo "$number is an integer";
}
else {
echo "$number is not an integer";
}
Because the actual declaration of that variable assigns a string valuealbeit one that contains a numberthe condition fails. Although $number in the previous example is a string, PHP is flexible enough to allow this value to be used in numeric operations. The following example shows that a string value that contains a number can be incremented and that the resulting value is an integer: $number = "6"; $number++; echo "$number has type " . gettype($number); Understanding NULLs
The value NULL is a data type all to itselfa value that actually has no value. It has no numeric value, but comparing to an integer value zero
$number = 0;
$empty=NULL;
if ($number == $empty) {
echo "The values are the same";
}
|
Numeric FunctionsLet's take a look at some of the numeric functions available in PHP.
Rounding
|
|
|
Negative Rounding
Note the way that negative numbers are rounded. The result of
floor(-1.1)
is
-2
the
|
To round a value to the nearest whole number, you use
round
. A
The round function can also take an optional precision argument. The following example displays a value rounded to two decimal places:
$score = 0.535; echo round($score, 2);
The value displayed is 0.54 ; the third decimal place being 5 causes the final digit to be rounded up.
You can also use round with a negative precision value to round an integer to a number of significant figures, as in the following example:
$distance = 2834; echo round($distance, -2);
To find the smallest and largest of a
This statement will display the larger of the two
echo max($a, $b);
There is no limit to the number of arguments that can be compared. The following example finds the lowest value from a larger set of values:
echo min(6, 10, 23, 3, 88, 102, 5, 44);
Not surprisingly, the result displayed is 3 .
You use
rand
to generate a random integer, using your system's built-in random number generator. The
rand
function
|
|
Random Limit The constant RAND_MAX contains the highest random number value that can be generated on your system. This value may vary between different platforms. |
The following statement picks a random number between 1 and 10 and displays it:
echo rand(1, 10);
You can put this command in a script and run it a few times to see that the number changes each time it is run.
There is really no such thing as a computer-generated random number. In fact, numbers are actually picked from a very long sequence that has very similar properties to true random numbers. To make sure you always start from a different place in this sequence, you have to
seed
the random number generator by calling the
|
|
Random Algorithms
PHP includes another random number generator, known as Mersenne Twister, that is
|
PHP includes many mathematical functions, including trigonometry, logarithms, and number base conversions. As you will rarely need to use these in a web environment, those functions are not covered in this book.
To find out about a function that