Skip to content
Related Articles

Related Articles

PHP Math Functions (abs, pi, deg2rad, rad2deg, fmod, floor, ceil, round, is_finite, is_infinite)
  • Last Updated : 09 Mar, 2018
GeeksforGeeks - Summer Carnival Banner

As you know PHP is a sever side script language and is used for creating dynamic web pages. PHP provides a number of built-in math functions which help in performing several operations while dealing with mathematical data. It has nice support for mathematical processing. No installation is required to use these functions.

Some examples of mathematical functions in PHP are as follows:

1. abs() :
This function takes negative value as input and returns the absolute (positive) value of a integer or float number.

Syntax :

abs(number);

In this function ‘number’ can be float or integer.



Example :




<?php
// PHP code to return absolute value.
function absolute($degree)
{
    return (abs($degree));
}
  
// Driver Code
$number = -8.4;
echo(absolute($number));
?>

Output :

8.4

 

2. pi() :
This function returns the value of pi. The named constant M_PI is identical to pi().

Syntax :

pi();

Example :




<?php
# PHP function to convert degree to radian value.
echo(pi());
?>

Output :

3.1415926535898

 
3. deg2rad() :
This function takes a degree value as input and convert it to a radian value.



Syntax :

deg2rad(number);

Example :




<?php
// PHP code to convert degree to radian value.
function deg_radian($degree)
{
    return (deg2rad($degree));
}
  
// Driver Code
$number = 180;
echo(deg_radian($number));
?>

Output :

3.1415926535898

 
4. rad2deg() :
This function takes a radian value as input and convert it to a degree value.

Syntax :

rad2deg(number);

Example :




<?php
// PHP function to convert degree to radian value.
echo(rad2deg(pi()));
?>

Output :

180

 

5. fmod() :
This function takes two arguments as input returns the floating point remainder (modulo) of division of arguments.

Syntax :

fmod(x, y);

Here, x is dividend and y is divisor. The remainder (r) is defined as: x = i * y + r, for some integer i. If y is non-zero, r has the same sign as x and a magnitude less than the magnitude of y.

Example :




<?php
// PHP code to find modulo of x / y
function result($x, $y)
{
    return fmod($x, $y);
}
  
// Driver function
$x = 5.7;
$y = 1.3;
echo(result($x, $y));
?>

Output :

0.5

Here, $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7 (x = i * y + r)
 

6. floor() :
This function takes numeric value as argument and returns the next lowest integer value (as float) by rounding down value if necessary.
Syntax :

floor($number);

Example :




<?php
echo(floor(0.60)."\n");
echo(floor(5)."\n");
echo(floor(-5.9));
?>

Output :

0
5
-6

 

7. ceil() :
This function takes numeric value as argument and returns the next highest integer value by rounding up value if necessary.

Syntax :

ceil($number);

Example :




<?php
echo(ceil(0.60)."\n");
echo(ceil(-5.9));
?>

Output :

1
-5

 

8. round() :
This function takes numeric value as argument and returns the next highest integer value by rounding up value if necessary.

Syntax :

round(number, precision, mode);

In this, number specifies the value to round, precision specifies the number of decimal digits to round to (Default is 0) and, mode(optional) specifies one of the following constants to specify the mode in which rounding occurs :

  • PHP_ROUND_HALF_UP : (set by Default) Rounds number up to precision decimal, when it is half way there. Rounds 1.5 to 2 and -1.5 to -2
  • PHP_ROUND_HALF_DOWN : Round number down to precision decimal places, when it is half way there. Rounds 1.5 to 1 and -1.5 to -1
  • PHP_ROUND_HALF_EVEN : Round number to precision decimal places towards the next even value.
  • PHP_ROUND_HALF_ODD : Round number to precision decimal places towards the next odd value.

Example :




<?php
echo(round(1.95583, 2)."\n");
echo(round(1241757, -3)."\n");
echo(round(9.5, 0, PHP_ROUND_HALF_UP)."\n");
echo(round(9.5, 0, PHP_ROUND_HALF_DOWN)."\n");
echo(round(9.5, 0, PHP_ROUND_HALF_EVEN)."\n");
echo round(9.5, 0, PHP_ROUND_HALF_ODD);
?>

Output :

1.96
1242000
10
9
10
9

 

9. is_finite() :
This function takes any value as argument and checks whether a value is finite or not. It returns TRUE (1) if the specified value is a finite number, otherwise it returns FALSE/NOTHING.
The value is a legal finite if it is within the allowed range for a PHP float on this platform.

Syntax :

is_finite(value);

Example :




<?php
echo is_finite(234)."\n";
echo is_finite(log(0));
?>

Output :

1

 

10. is_infinite() :
This function takes any value as argument and checks whether a value is infinite or not. It returns TRUE (1) if the specified value is a infinite number, otherwise it returns FALSE/NOTHING.
The value is a legal infinite if it is outside the allowed range for a PHP float on this platform.

Syntax :

is_infinite(value);

Example :




<?php
echo is_infinite(234)."\n";
echo is_infinite(log(0));
?>

Output :

1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :