2.9. Integer and Float Functions
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.
2.9.1. Absolute Value
integer abs(integer number) float abs(float number)
The following examples show the result of abs( ) on floats and integers:
echo abs(-1); // prints 1 echo abs(1); // prints 1 echo abs(-145.89); // prints 145.89 echo abs(145.89); // prints 145.89
2.9.2. Ceiling and Floor
float ceil(float value) float floor(float value)
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:
echo ceil(27.3); // prints 28 echo floor(27.3); // prints 27
float round(float value [, integer precision])
Rounding by default 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. The following examples show rounding at various precisions:
echo round(10.4); // prints 10 echo round(10.5); // prints 11 echo round(2.40964, 3); // prints 2.410 echo round(567234.56, -3); // prints 567000 echo round(567234.56, -4); // prints 570000
2.9.4. Number Systems
string decbin(integer number) integer bindec (string binarystring) string dechex(integer number) integer hexdec(string hexstring) string decoct(integer number) integer octdec(string octalstring)
The decimal numbers are always treated as integers, and the numbers in the other systems are treated as strings. When converting to decimal, care must be taken that the source number isn't greater than the maximum value an integer can hold. Here are some examples:
echo decbin(45); // prints "101101" echo bindec("1001011"); // prints 75 echo dechex(45); // prints "2D" echo hexdec("5a7b"); // prints 23163 echo decoct(45); // prints "55" echo octdec("777"); // prints 511
2.9.5. Basic Trigonometry Functions
PHP supports the basic set of trigonometry functions and are listed in Table 2-4.
Table 2-4. Trigonometry functions supported by PHP
2.9.6. Powers and Logs
The PHP mathematical library includes the exponential and logarithmic functions listed in Table 2-5.
Table 2-5. Exponential and logarithmic functions
2.9.7. Random Number Generation
PHP provides the function rand( ), which returns values from a generated sequence of pseudo-random numbers. Well-known algorithms generate sequences that appear to have random behavior but aren't truly random. The srand( ) function seeds the algorithm and needs to be called before the first use of the rand( ) function in a script. Otherwise, the function returns the same numbers each time a script is called. The prototypes of the functions are:
void srand(integer seed) integer rand( ) integer rand(integer min, integer max)
The srand( ) function is called by passing an integer seed that is usually generated from the current time. 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 this example:
// Generate a seed. $seed = (float) microtime( ) * 100000000; // Seed the pseudo-random number generator srand($seed); // Generate some random numbers print rand(); // between 0 and getmaxrand( ) print rand(1, 6); // between 1 and 6 (inclusive)
Copyright © 2003 O'Reilly & Associates. All rights reserved.