Math Functions in PHP:Part 3


Introduction

The PHP math functions are used to handle values within the range of integer and float types. In this article I describe some more of the PHP math functions. Here I explain some Math functions and I also describe some Math functions in Part1 and Part2.

PHP Rand() function

The PHP rand function generates a random integer. If you call rand without a parameter then it will return a random integer between 0 and RAND_MAX.

Syntax

rand  (min, max)


Parameters in Rand function

It has two parameters; they are:
 

Parameter Description
min It specifies minimum range as a integer.
max It specifies maximum range as a integer.

Example of Rand function 

<?php

echo ("Random number without the range: ". rand() ."</br>");

echo ("Random number between 10 to 100: ".rand(10,100)."</br>");

?>

Output


math-rand-function-in-php.jpg

PHP Round() function


It rounds a number to the nearest integer.

Syntax

round  (number, round decimal)

 

Parameters in Round function

It has two parameters; they are:
 

Parameter Description
number The no to be round and it is required parameter.
round decimal It is optional parameter and specifies number of digests after the decimal point.

Example of Round function

<?
php

echo ("Round of (0.70)  is:  " .round(0.70)."</br>");

echo ("Round of (0.40)  is:  " .round(0.40)."</br>");

echo ("Round of (13,5)  is:  " .round(13,5)."</br>");

echo ("Round of (-0.70)  is: " .round(-0.70)."</br>");

echo ("Round of (3.37)  is:  " .round(3.37)."</br>");

?>

Output

math-roundfunction-in-php.jpg

PHP Exp() function

It calculates the exponent of e (e is the base of the natural number system of logarithms, or approximately 2.718282).

Syntax

exp  (number)


Parameters in
Exp
function

It contains one parameter; it is:
 

Parameter Description
number It specifies a number.


Example of Exp function

<?php

echo ("exp of 0 is ".exp(0)."</br>");

echo ("exp of -2 is ".exp(-2)."</br>");

echo ("exp of 3 is ".exp(3)."</br>");

echo ("exp of 3.3 is ".exp(3.3)."</br>");

?>

Output

 math-exp-function-in-php.jpg


Similar Articles