Math function in php

Today, We want to share with you math function in php.In this post we will show you numeric function in php, hear for array functions in php we will give you demo and example for implement.In this post, we will learn about Introduction To Math Class in C# ASP.NET with an example.

And Today, we will discuss – “What are PHP MATH Functions?”

PHP Math Functions

As you know PHP is a sever side script language which is used for making the dynamic web pages.

There are various mathematical functions in PHP. PHP Math Function can be used to perform mathematical operations like as a max(), min(), round() or addition, subtraction, division and multiplication and many other mathematical requirements.

This all the functions can handle values within the range of integer as well as float types.

PHP has nice support for mathematical processing.

Golang Float64s function - Math function
Golang Float64s function – Math function

PHP Math Functions: Main Tips

  • PHP math functions are used for handling float and integer variables.
  • These functions are inbuilt into PHP.

Write PHP script to demonstrate math functions.

<?php
 $first_no=500;
 $second_no=250;
 $n3=min($first_no,$second_no);
 echo "Min is : $n3"."<br><br>";
 $first_no=max($first_no,$n3);
 echo "Max is : $first_no"."<br><br>";
 $n4=sqrt($first_no);
 echo "sqrt is  : $n4"."<br><br>";
 $first_no=pow($second_no,$n4);
 echo "pow is : $first_no"."<br><br>";
 echo "ceil : ".ceil($first_no)."<br><br>";
 echo "floor : ".floor($second_no)."<br><br>";
 echo "round : ".round($n3.$first_no)."<br><br>";
 echo "abs : ".abs($n4);
 
?>

Following is an explanation of some interesting PHP Math Functions –

abs () function

It is a Give backs the absolute (positive) value of a number.

<html>  
<body style="background-color:grey;">  
<h3>abs function</h3>  
<?php  
$num=-10;  
echo "User Input number is : $num".'<br>';  
echo "This abs function User Input is : ".abs ($num);  
?>  
</body>  
</html>  

<?php  
echo (abs(-7)."<br/>"); // 7 (integer)  
echo (abs(7)."<br/>"); //7 (integer)  
echo (abs(-7.2)."<br/>"); //7.2 (float/double)  
?>

dechex () function

It is a Converts a decimal number to a hexadecimal number.

<html>  
<body style="background-color:grey;">  
<h3>dechex function</h3>  
<?php  
$num=525;  
echo "User Input number is : $num".'<br>';  
echo "This dechex function User Input is : ".dechex ($num);  
?>  
</body>}  
</html>  

sqrt() function

It is a Give backs the square root of a number.

<html>  
<body style="background-color:grey;">  
<h3>sqrt  function</h3>  
<?php  
$num=625;  
echo "User Input number is : $num".'<br>';  
echo "This sqrt function User Input is : ".sqrt ($num);  
?>  
</body>  
</html> 

<?php  
echo (sqrt(16)."<br/>");// 4  
echo (sqrt(25)."<br/>");// 5  
echo (sqrt(7)."<br/>");// 2.6457513110646  
?>

log() function

It is a Give backs the natural logarithm of a number.

<html>  
<body style="background-color:grey;">  
<h3>log  function</h3>  
<?php  
$num=10;  
echo "User Input number is : $num".'<br>';  
echo "This log function User Input is : ".log ($num);  
?>  
</body>  
</html> 

floor() function

It is a Rounds a number down to the nearest integer.

<html>  
<body style="background-color:grey;">  
<h3>floor  function</h3>  
<?php  
$num=5.89;  
echo "User Input number is : $num".'<br>';  
echo "This floor function User Input is : ".floor ($num).'<br>';  
?>  
</body>  
</html>  

<?php  
echo (floor(3.3)."<br/>");// 3  
echo (floor(7.333)."<br/>");// 7  
echo (floor(-4.8)."<br/>");// -5  
?>

ceil() function

It is a Rounds a number up to the nearest integer.

<html>  
<body style="background-color:grey;">  
<h3>ceil  function</h3>  
<?php  
$num=5.89;  
echo "User Input number is : $num".'<br>';  
echo "This ceil function User Input is : ".ceil ($num).'<br>';  
?>  
</body>  
</html>  

<?php  
echo (ceil(3.3)."<br/>");// 4  
echo (ceil(7.333)."<br/>");// 8  
echo (ceil(-4.8)."<br/>");// -4  
?>

pow() function

It is a Give backs x raised to the power of y.

<html>  
<body style="background-color:grey;">  
<h3>pow  function</h3>  
<?php  
$num=5;  
echo "User Input number is : $num".'<br>';  
echo "This pow function User Input is : ".pow(2,$num).'<br>';  
echo "This pow function User Input is : ".pow($num,2);  
?>  
</body>  
</html>  

max() and min() function

These are Give backs the highest value in an array, or the highest value of several specified values as well as Give backs the lowest value in an array, or the lowest value of several specified values.

<html>  
<body style="background-color:grey;">  
<h3>max and min  function</h3>  
<?php  
$num=array(2,6,10,3,15);  
Echo "User Input number are :" ." ". $num[0].",".$num[1].",".$num[2].",".$num[3].",".$num[4].'<br>';  
echo "Maximum number  is : ".max ($num).'<br>';  
echo "Minimum number is : ".min ($num);  
?>  
</body>  
</html>  

sin() function

It is a Give backs the sine of a number.

<html>  
<body style="background-color:grey;">  
<h3>sin()  function</h3>  
<?php  
$num=0;  
Echo "Data Content of the angle : $num".'<br>';  
echo "Data Content of the sin0 is : ".sin($num);  
?>  
</body>  
</html>  

cos() function

It is a Give backs the cosine of a number.

<html>  
<body style="background-color:grey;">  
<h3>cos()  function</h3>  
<?php  
$num=0;  
Echo "Data Content of the angle : $num".'<br>';  
echo "Data Content of the cos0 is : ".cos($num);  
?>  
</body>  
</html> 

tan() function

It is a Give backs the tangent of a number.

<html>  
<body style="background-color:grey;">  
<h3>tan()  function</h3>  
<?php  
$num=45;  
Echo "Data Content of the angle : $num".'<br>';  
echo "Data Content of the tan45 is : ".tan ($num);  
?>  
</body>  
</html>  

Mathematical Functions in PHP

In the table below, Let’s see the list of important PHP math functions. All the PHP math functions are listed alphabetically.

Read Also:  apache2 enable php - How to Enable Php In Apache?
Function Description
abs() Give backs the absolute (positive) value of a number
acos() Give backs the arc cosine of a number
acosh() Give backs the inverse hyperbolic cosine of a number
asin() Give backs the arc sine of a number
asinh() Give backs the inverse hyperbolic sine of a number
atan() Give backs the arc tangent of a number in radians
atan2() Give backs the arc tangent of two variables x and y
atanh() Give backs the inverse hyperbolic tangent of a number
base_convert() Converts a number from one number base to another
bindec() Converts a binary number to a decimal number
ceil() Rounds a number up to the nearest integer
cos() Give backs the cosine of a number
cosh() Give backs the hyperbolic cosine of a number
decbin() Converts a decimal number to a binary number
dechex() Converts a decimal number to a hexadecimal number
decoct() Converts a decimal number to an octal number
deg2rad() Converts a degree value to a radian value
exp() Calculates the exponent of e
expm1() Give backs exp(x) – 1
floor() Rounds a number down to the nearest integer
fmod() Give backs the remainder of x/y
getrandmax() Give backs the largest possible value returned by rand()
hexdec() Converts a hexadecimal number to a decimal number
hypot() Calculates the hypotenuse of a right-angle triangle
intdiv() Performs integer division
is_finite() Checks whether a value is finite or not
is_infinite() Checks whether a value is infinite or not
is_nan() Checks whether a value is ‘not-a-number’
lcg_value() Give backs a pseudo random number in a range between 0 and 1
log() Give backs the natural logarithm of a number
log10() Give backs the base-10 logarithm of a number
log1p() Give backs log(1+number)
max() Give backs the highest value in an array, or the highest value of several specified values
min() Give backs the lowest value in an array, or the lowest value of several specified values
mt_getrandmax() Give backs the largest possible value returned by mt_rand()
mt_rand() Generates a random integer using Mersenne Twister algorithm
mt_srand() Seeds the Mersenne Twister random number generator
octdec() Converts an octal number to a decimal number
pi() Give backs the value of PI
pow() Give backs x raised to the power of y
rad2deg() Converts a radian value to a degree value
rand() Generates a random integer
round() Rounds a floating-point number
sin() Give backs the sine of a number
sinh() Give backs the hyperbolic sine of a number
sqrt() Give backs the square root of a number
srand() Seeds the random number generator
tan() Give backs the tangent of a number
tanh() Give backs the hyperbolic tangent of a number

I hope you get an idea about What is function in general mathematics in PHP?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.