Lesson 6: PHP Math Functions - TOP10

Lesson 6: PHP Math Functions

PHP is a very powerful scripting language. It has a lot of built in functions which help you do stuff much faster than it would take if you coded those functions yourself. There are some really good functions in PHP which you can use for mathematical operations. In this Lesson, I will introduce some really helpful math functions and explain how you can use them in your PHP program.

Power Function - pow() Syntax: pow(a,b) This will return the value a ^ b. Example: pow(2,4) returns 16 Pi Function - pi() Syntax: pi() This will return the value of the mathematical constant Pi: 3.141592 Random Function - rand() Syntax: rand() This will return any random integer. Random Function - rand() Syntax: rand() This will return any random integer. Example: rand() may return 1254 Optional Syntax: rand(minimum, maximum) This will return a random number in the range minimum to maximum. Example: rand (10,20) may return any number between 10 and 20. Maximum Function - max() Syntax: max(a,b) This will return the maximum value in a or b. Example: max(5,10) will return 10 Minimum Function - min() Syntax: min(a,b) This will return the minimum value in a or b. Example: min(5,10) will return 5 Square Root Function - sqrt() Syntax: sqrt(a) This will return the square root of a. Example: sqrt(9) will return 3 Logarithm Function - log() Syntax: log(a,x) This will return the value of log a to the base x. If no base is specified, the base is assumed to be "e". Examples: log(2) returns 0.6931 log(2,2) returns 1 log(2.718) returns 1.000000xxx (a the value of E is 2.718)

Exponent Function - exp()

Syntax: exp(a) This will return the value of e raised to the ath power. Example: exp(2) will return 2.718 ^ 2 = 7.3890561 Absolute Value function - abs() Syntax: abs(a) This will return the absolute value of a. Example: abs(-5) returns 5, abs(10) returns 10 Rounding Function - round() Syntax: round(a) This will round the number a to its nearest integer value. Example: round(1.4) returns 1 round(1.6) returns 2 Ceiling Function - ceil() Syntax: ceil(a) This will return the smallest integer greater than the value a. Example: ceil(1.4) returns 2 ceil(-1.4) returns -1 Floor Function - floor() Syntax: floor(a) This will return the biggest integer smaller than the value a. Examples: ceil(1.4) returns 1 ceil(-1.4) returns -2 Sine Function - sin() Syntax: sin(a) This will return the sine value of a. Example: sin(2) returns 0.9092 Cosine Function - cos() Syntax: cos(a) This will return the cosine value of a. Example: cos(2) returns -0.4161 Tangent Function - tan() Syntax: tan(a) This will return the tangent value of a. Example: tan(2) returns -2.185 Binary to Decimal Function - bindec() Syntax: bindec(binary_a) This returns the decimal value of the binary number a. Example: bindec("111") returns 7 Hexadecimal to Decimal Function - hexdec() Syntax: hexdec(hex_a) This returns the decimal value of the hexadecimal number a. Example: hexdec("11") returns 17 Similarly there are also functions to convert decimal to hexadecimal (dechex) and binary (decbin). Formatting a number - number_format() Syntax: number_format ( float $number [, int $decimals = 0 ] )

Syntax: number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' ) This function accepts either one, two, or four parameters (not three): If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands. If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every group of thousands. If all four parameters are given, number will be formatted with decimals decimals, dec_point instead of a dot (".") before the decimals and thousands_sep instead of a comma (",") between every group of thousands.


Example: $number = 1234.56; // english notation (default) $english_format_number = number_format($number); echo $english_format_number . "
"; // 1,235 // French notation $nombre_format_francais = number_format($number, 2, ',', ' '); echo $nombre_format_francais . "
"; // 1 234,56 $number = 1234.5678; // english notation without thousands seperator $english_format_number = number_format($number, 2, '.', ''); echo $english_format_number . "
"; // 1234.57


Previous
Next Post »