Math Functions in PHP:Part 2

Introduction

The PHP math functions are used to handle values within the range of integer and float types. In this article I am describing some of PHP math functions and I am also describes some PHP math functions in PART1.

PHP Ceil() function

The PHP math ceil function rounds (in other words returns the value of a number) the value "upwards" to the nearest integer.

Syntax

ceil  (number)

Parameters in Ceil function

It contains one parameter.
 

Parameter Description
number It specifies a number to round.

Example of Ceil function

<
html>
<
body>
<?
php
$
ceil1=ceil(4.3);
$
ceil2=ceil(-4.3);
$
ceil3=ceil(4.335);
$
ceil4=ceil(9.4);
$
ceil5=ceil(9.9);
$
ceil6=ceil(4000.31);
echo
"Result of ceil<b> (4.3)</b> is : ". $ceil1. "</br>";
echo
"Result of ceil <b>(-4.3) is</b> : ". $ceil2."</br>";
echo
"Result of ceil <b>(4.355) is</b> : ". $ceil3."</br>";
echo
"Result of ceil <b>(9.4) is</b> : ". $ceil4."</br>";
echo
"Result of ceil <b>(9.9) is</b> : ". $ceil5."</br>";
echo
"Result of ceil <b>(4005.31)</b> is : ". $ceil6."</br>";
?>
</
body>
</
html>

Note:
 In the example above the ceil function takes as its argument a numeric expression. For example ceil(4.3) returns 5, because 5 is the smallest integer that is larger than or equal to the CEIL argument of 4.3. 

Output

math-ceil-function-in-php.jpg

PHP Floor() function

The PHP math floor function rounds off (in other words returns a value of a number) the value "downwards" to the nearest integer.

Syntax

floor  (number)


Parameters in Floor function

It contains one parameter.
 

Parameter Description
number It specifies a number to round.

Example of Floor function

<?php

$floor1=floor(4.3);

$floor2=floor(-4.3);

$floor3=floor(4.335);

$floor4=floor(9.4);

$floor5=floor(9.9);

$floor6=floor(4000.31);

echo "Result of floor<b> (4.3)</b> is : ". $floor1. "</br>";

echo "Result of floor <b>(-4.3) is</b> : ". $floor2."</br>";

echo "Result of floor <b>(4.355) is</b> : ". $floor3."</br>";

echo "Result of floor <b>(9.4) is</b> : ". $floor4."</br>";

echo "Result of floor <b>(9.9) is</b> : ". $floor5."</br>";

echo "Result of floor <b>(4005.31)</b> is : ". $floor6."</br>";

?>

Note:
In the example above the floor function takes as its argument a  numeric expression. For example floor(4.3) returns 4 because 4 is the largest integer that is smaller than or equal to the FLOOR argument of 4.3.

Output

math-floor-function-in-php.jpg

PHP Pow() function


It raises the first number to the power of the second number.

Syntax

pow (x, y)


Parameters in Pow function

It has two parameters.
 

Parameter Description
x It specifies the number to be raised
y It specifies the power to which to raise the number.

Example of Pow function

<?php

$pow1=pow(4,2);

$pow2=pow(-2,3.3);

$pow3=pow(-4,3);

$pow4=pow(-2,-3);

$pow5=pow(9,2.5);

echo "Result of pow<b> (4,2)</b> is : ". $pow1. "</br>";

echo "Result of pow <b>(-2,3.3) is</b> : ". $pow2."</br>";

echo "Result of pow <b>(-4,3) is</b> : ". $pow3."</br>";

echo "Result of pow <b>(-2,-3) is</b> : ". $pow4."</br>";

echo "Result of pow <b>(9,2.5) is</b> : ". $pow5."</br>";

?>

Note:
In the example above the pow function raises its first numeric arguments. For example pow(4,2) returns 16. If in the pow function the first numeric value is negative and the second is positive but is not an integer then it returns NaN (not a number).

Output

math-pow-function-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all