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:
 

abs () function

 
The abs() function returns the absolute value of a number. The example of the abs() function is as follows:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>abs function</h3>  
  4. <?php  
  5. $num=-10;  
  6. echo "Your number is : $num".'<br>';  
  7. echo "Using abs function your number is : ".abs ($num);  
  8. ?>  
  9. </body>  
  10. </html>  
Output
 
abs function in php 
 

dechex () function

 
The dechex() function is used to convert the decimal number to a hexadecimal number. The example of hecdex() function is as follows:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>dechex function</h3>  
  4. <?php  
  5. $num=525;  
  6. echo "Your number is : $num".'<br>';  
  7. echo "Using dechex function your number is : ".dechex ($num);  
  8. ?>  
  9. </body>}  
  10. </html>  
Output
 
dechex function in php 
 

sqrt() function

 
The sqrt() function is used to get the square root of a number. The example of the sqrt() function is as follows:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>sqrt  function</h3>  
  4. <?php  
  5. $num=625;  
  6. echo "Your number is : $num".'<br>';  
  7. echo "Using sqrt function your number is : ".sqrt ($num);  
  8. ?>  
  9. </body>  
  10. </html>  
Output
 
sqrt function in php 
 

log() function

 
The log() function is used to get the log value of a number. The example of the log() function is as follows:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>log  function</h3>  
  4. <?php  
  5. $num=10;  
  6. echo "Your number is : $num".'<br>';  
  7. echo "Using log function your number is : ".log ($num);  
  8. ?>  
  9. </body>  
  10. </html>  
Output
 
log function in php 
 

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:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>floor  function</h3>  
  4. <?php  
  5. $num=5.89;  
  6. echo "Your number is : $num".'<br>';  
  7. echo "Using floor function your number is : ".floor ($num).'<br>';  
  8. ?>  
  9. </body>  
  10. </html>  
Output
 
floor function in php 
 

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:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>ceil  function</h3>  
  4. <?php  
  5. $num=5.89;  
  6. echo "Your number is : $num".'<br>';  
  7. echo "Using ceil function your number is : ".ceil ($num).'<br>';  
  8. ?>  
  9. </body>  
  10. </html>  
Output
 
ceil function in php 
 

pow() function

 
The pow() is used to calculate the power of any number. The example of the pow() is as follows:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>pow  function</h3>  
  4. <?php  
  5. $num=5;  
  6. echo "Your number is : $num".'<br>';  
  7. echo "Using pow function your number is : ".pow(2,$num).'<br>';  
  8. echo "Using pow function your number is : ".pow($num,2);  
  9. ?>  
  10. </body>  
  11. </html>  
Output
 
pow function in php 
 

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:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>max and min  function</h3>  
  4. <?php  
  5. $num=array(2,6,10,3,15);  
  6. Echo "Your number are :" ." "$num[0].",".$num[1].",".$num[2].",".$num[3].",".$num[4].'<br>';  
  7. echo "Maximum number  is : ".max ($num).'<br>';  
  8. echo "Minimum number is : ".min ($num);  
  9. ?>  
  10. </body>  
  11. </html>  
Output
 
max and min function in php 
 

Geometric Functions

 

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:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>sin()  function</h3>  
  4. <?php  
  5. $num=0;  
  6. Echo "The value of angle : $num".'<br>';  
  7. echo "The value of sin0 is : ".sin($num);  
  8. ?>  
  9. </body>  
  10. </html>  
Output
 
sin function in php 
 

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:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>cos()  function</h3>  
  4. <?php  
  5. $num=0;  
  6. Echo "The value of angle : $num".'<br>';  
  7. echo "The value of cos0 is : ".cos($num);  
  8. ?>  
  9. </body>  
  10. </html>  
Output
 
cos function in php 
 

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:
  1. <html>  
  2. <body bgcolor="pink">  
  3. <h3>tan()  function</h3>  
  4. <?php  
  5. $num=45;  
  6. Echo "The value of angle : $num".'<br>';  
  7. echo "The value of tan45 is : ".tan ($num);  
  8. ?>  
  9. </body>  
  10. </html>  
Output
 
tan function in php 
 
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