Mathematical Functions in PHP


Introduction

As you know PHP is a sever side script language which is used for creating dynamic web pages. In PHP you can use mathematical functions. There are various mathematical functions in PHP. There are PHP math functions to take care of addition, subtraction, division and multiplication and many other mathematical requirements. PHP has nice support for mathematical processing. Some examples of mathematical functions in PHP are as follows:

1. abs () function

The abs() function returns the absolute value of a number. The example of the abs() function is as follows:

<html>
<
body bgcolor="pink">
<h3>abs function</h3>
<?php
$num=-10;
echo "Your number is : $num".'<br>';
echo "Using abs function your number is : ".abs ($num);
?>
</body>
</html>

Output

image1.jpg

2. dechex () function

The dechex() function is used to convert the decimal number to a hexadecimal number. The example of hecdex() function is as follows:

<html>
<
body bgcolor="pink">
<h3>dechex function</h3>
<?php
$num=525;
echo "Your number is : $num".'<br>';
echo "Using dechex function your number is : ".dechex ($num);
?>
</body>}
</html>

Output

image2.jpg

3. sqrt() function

The sqrt() function is used to get the square root of a number. The example of the sqrt() function is as follows:

<html>
<
body bgcolor="pink">
<h3>sqrt  function</h3>
<?php
$num=625;
echo "Your number is : $num".'<br>';
echo "Using sqrt function your number is : ".sqrt ($num);
?>
</body>
</html>

Output

image3.jpg

4. log() function

The log() function is used to get the log value of a number. The example of the log() function is as follows:

<html>
<
body bgcolor="pink">
<h3>log  function</h3>
<?php
$num=10;
echo "Your number is : $num".'<br>';
echo "Using log function your number is : ".log ($num);
?>
</body>
</html>

Output

image4.jpg

5. floor() function

In the floor() function we use a float or real number. The floor() function returns the nearest value of an integer. The example of the floor() function is as follows:

<html>
<
body bgcolor="pink">
<h3>floor  function</h3>
<?php
$num=5.89;
echo "Your number is : $num".'<br>';
echo "Using floor function your number is : ".floor ($num).'<br>';
?>
</body>
</html>

Output

image5.jpg

6. ceil() function

In the ceil() function we use a float or real number. The ceil() function returns the nearest value of an integer. The example of the ceil() function is as follows:

<html>
<
body bgcolor="pink">
<h3>ceil  function</h3>
<?php
$num=5.89;
echo "Your number is : $num".'<br>';
echo "Using ceil function your number is : ".ceil ($num).'<br>';
?>
</body>
</html>

Output

image6.jpg

7. pow() function

The pow() is used to calculate the power of any number. The example of the pow() is as follows:

<html>
<
body bgcolor="pink">
<h3>pow  function</h3>
<?php
$num=5;
echo "Your number is : $num".'<br>';
echo "Using pow function your number is : ".pow(2,$num).'<br>';
echo "Using pow function your number is : ".pow($num,2);
?>
</body>
</html>

Output

image7.jpg

8. max() and min() function

The max() function is used to calculate the maximum number of various numbers. The min() function is used to calculate minimum number of various numbers. The example of the max() and min() functions is as follows:

<html>
<
body bgcolor="pink">
<h3>max and min  function</h3>
<?php
$num=array(2,6,10,3,15);
Echo "Your 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>

Output

image8.jpg

Geometric Functions

1. sin() function

The sin() function is used to calculate the sine value of a given number. The example of the sin() function is as follows:

<html>
<
body bgcolor="pink">
<h3>sin()  function</h3>
<?php
$num=0;
Echo "The value of angle : $num".'<br>';
echo "The value of sin0 is : ".sin($num);
?>
</body>
</html>

Output

image9.jpg

2. cos() function

The cos() function is used to calculate the cosine value of a given number. The example of the cos() function is as follows:

<html>
<
body bgcolor="pink">
<h3>cos()  function</h3>
<?php
$num=0;
Echo "The value of angle : $num".'<br>';
echo "The value of cos0 is : ".cos($num);
?>
</body>
</html>

Output

image10.jpg

3. tan() function

The tan() function is used to calculate the tangent value of a given number. The example of the tan() function is as follows:

<html>
<
body bgcolor="pink">
<h3>tan()  function</h3>
<?php
$num=45;
Echo "The value of angle : $num".'<br>';
echo "The value of tan45 is : ".tan ($num);
?>
</body>
</html>

Output

image11.jpg

Similarly you can use more geometric function in PHP.

Conclusion

So in this article you saw how to use mathematical and geometric functions in PHP. Using this article one can easily understand the use of mathematical functions in PHP.

Some Useful Resources

Up Next
    Ebook Download
    View all
    Learn
    View all