In this tutorial, you will learn about PHP Math step by step. So, without much to do, let’s get started.
PHP has a set of math functions that allows you to perform mathematical tasks on numbers.
In this tutorial, you will learn-
In this article, you will learn-
PHP pi() Function
The pi() function returns the value of PI:
Example
<!DOCTYPE html> <html> <body> <?php echo(pi()); ?> </body> </html>
PHP min() and max() Functions
The min() and max() functions can be used to find the lowest or highest value in a list of arguments:
Example
<!DOCTYPE html> <html> <body> <?php echo(min(0, 150, 30, 20, -8, -200) . "<br>"); echo(max(0, 150, 30, 20, -8, -200)); ?> </body> </html>
PHP abs() Function
The abs() function returns absolutely the (positive) value of a number:
Example
<!DOCTYPE html> <html> <body> <?php echo(abs(-6.7)); ?> </body> </html>
PHP sqrt() Function
The sqrt() feature returns the square root of a variety of:
Example
<!DOCTYPE html> <html> <body> <?php echo(sqrt(64) . "<br>"); echo(sqrt(0) . "<br>"); echo(sqrt(1) . "<br>"); echo(sqrt(9)); ?> </body> </html>
PHP round() Function
The round() function rounds a floating-point number to its nearest integer:
Example
<!DOCTYPE html> <html> <body> <?php echo(round(0.60) . "<br>"); echo(round(0.50) . "<br>"); echo(round(0.49) . "<br>"); echo(round(-4.40) . "<br>"); echo(round(-4.60)); ?> </body> </html>
Random Numbers
The rand() function generates a random number:
Example
<!DOCTYPE html> <html> <body> <?php echo(rand()); ?> </body> </html>
To get more control over the random number, you could add the elective min and max parameters to specify the lowest integer and the highest integer to be returned.
For example, if you want a random integer among 10 and 100 (inclusive), use rand(10, 100):
Example
<!DOCTYPE html> <html> <body> <?php echo(rand(10, 100)); ?> </body> </html>
READ NEXT
This is about PHP Math, and we hope you have learned something from this tutorial and share your opinion about this tutorial. What do you think about it, and if you think this tutorial will help some of your friends, do share it with them.
PHP Tutorial (worldofitech.com)