As we discussed in Chapter 2, PHP supports both integer and floating-point numbers. PHP stores integers as a 32-bit signed word, providing a range of integers from -2147483647 to +2147483647. PHP automatically converts numbers that overflow out of this range to floats. You can see this behavior by adding one to the largest integer value:
// Largest positive integer (for a 32 bit signed integer) $variable = 2147483647; // prints int(2147483647) var_dump($variable); $variable++; // prints float(2147483648) var_dump($variable);
Floating-point numbers can store a wide range of values, both very large and very small, by storing a mantissa and an exponent. However a floating-point number can't precisely represent all numbers?for example, the fraction 2/3?and some precision can be lost.
Integers can be represented in a decimal, hexadecimal, or octal notation:
$var = 42; // a positive integer $var = -186; // a negative integer $var = 0654; // 428 expressed as an octal number $var = 0xf7; // 247 expressed as a hexadecimal number
Floating-point numbers can represented in a decimal or exponential notation:
$var = 42.0; // a positive float $var = -186.123; // a negative float $var = 1.2e65; // a very big number $var = 10e-75; // a very small number
Apart from the basic operators +, -, /, *, and %, PHP provides the usual array of mathematical library functions. In this section, we present some of the library functions that are used with integer and float numbers.
The absolute value of an integer or a float can be found with the abs( ) function:
The following examples show the result of abs( ) on integers and floats:
print abs(-1); // prints 1 print abs(1); // prints 1 print abs(-145.89); // prints 145.89 print abs(145.89); // prints 145.89
The ceil( ) and floor( ) functions return the integer value above and below a fractional value, respectively:
The return type is a float because an integer may not be able to represent the result when a large value is passed as an argument. Consider the following examples:
print ceil(27.3); // prints 28 print floor(27.3); // prints 27
The round( ) function uses 4/5 rounding rules to round up or down a value to a given precision:
By default, rounding is to zero decimal places, but the precision can be specified with the optional precision argument. The 4/5 rounding rules determine if a number is rounded up or down based on the digits that are lost due to the rounding precision. For example, 10.4 rounds down to 10, and 10.5 rounds up to 11. Specifying a negative precision rounds a value to a magnitude greater than zero, for example a precision of -3 rounds a value to the nearest thousand. The following examples show rounding at various precisions:
print round(10.4); // prints 10 print round(10.5); // prints 11 print round(2.40964, 3); // prints 2.410 print round(567234.56, -3); // prints 567000 print round(567234.56, -4); // prints 570000
PHP provides the following functions that convert numbers between integer decimal and the commonly used number systems, binary, octal, and hexadecimal:
The decimal numbers are always treated as integers, and the numbers in the other systems are treated as strings. Here are some examples:
print decbin(45); // prints "101101" print bindec("1001011"); // prints 75 print dechex(45); // prints "2D" print hexdec("5a7b"); // prints 23163 print decoct(45); // prints "55" print octdec("777"); // prints 511
It is possible to represent binary, octal, and hexadecimal numbers that are bigger than can be held in a 32-bit integer. The results of such conversions automatically overflow to a float value. For example:
// $a is an integer assigned the largest possible value $a = hexdec("7fffffff"); // $a is a float $a = hexdec("80000000");
PHP supports the basic set of trigonometry functions listed in Table 3-5.
Function |
Description |
---|---|
float sin(float arg) |
Sine of arg in radians |
float cos(float arg) |
Cosine of arg in radians |
float tan(float arg) |
Tangent of arg in radians |
float asin(float arg) |
Arc sine of arg in radians |
float acos(float arg) |
Arc cosine of arg in radians |
float atan(float arg) |
Arc tangent of arg in radians |
float atan2(float y, float x) |
Arc tangent of x/y where the sign of both arguments determines the quadrant of the result |
float pi( ) |
Returns the value 3.1415926535898 |
float deg2rad(float arg) |
Converts arg degrees to radians |
float rad2deg(float arg) |
Converts arg radians to degrees |
The PHP mathematical library includes the exponential and logarithmic functions listed in Table 3-6.
Function |
Description |
---|---|
float exp(float arg) |
e to the power of arg |
float pow(float base, number exp) |
Exponential expression base to the power of exp |
float sqrt(float arg) |
Square root of arg |
float log(float arg [, float base]) |
Natural logarithm of arg, unless a base is specified, in which case the function returns log(arg)/log(base) |
float log10(float arg) |
Base-10 logarithm of arg |
Many of the functions described in this section can return values that are undefined, or are too big or small to hold in a floating point number. PHP provides three functions that can be used to test numeric results before they cause problems later in a script:
The function is_nan( ) tests the expression val and returns true if val is not a number. For example, the square root of a negative number is not a real number. is_finite( ) returns true if the number val can be represented as a valid float, and is_infinite( ) returns true if val can't. Here are some examples:
// square root of a negative number $a = -1; $result = sqrt($a); print $result; // prints -1.#IND // Test if not a number if (is_nan($result)) print "Result not defined"; // prints else print "Square root of {$a} = {$result}";
PHP provides the function rand( ), which returns values from a generated sequence of pseudo-random numbers. The sequence generated by rand( ) is pseudo random because the algorithm used appears to have random behavior but isn't truly random. The function rand( ) can be called in one of two ways:
When called with no arguments, rand( ) returns a random number between 0 and the value returned by getrandmax( ) . When rand( ) is called with two arguments, the min and max values, the returned number is a random number between min and max. Consider an example:
// Generate some random numbers print rand( ); // between 0 and getmaxrand( ) print rand(1, 6); // between 1 and 6 (inclusive)
Prior to PHP 4.2.0, you needed to seed the random number generator with a call to srand( ) before the first use of rand( ), otherwise the function returns the same numbers each time a script is called. Since 4.2.0, the call to srand( ) is not required, however you can reliably reproduce a random sequence by calling srand( ) with the same argument at the start of the script. The following example reliably prints the same sequence of numbers each time it is called:
srand(123456); // Prints six random numbers for ($i=0; $i<6; $i++) print rand( ) . " ";