PHP math commonly used functions
In PHP programs, data processing operations are often required, which requires the use of mathematical functions. Mathematical functions are the simplest and most commonly used system functions. This chapter takes mathematical functions as an example to explain how to call system functions.
Let’s talk about our most commonly used mathematical functions now. Before explaining mathematical functions, let's talk about one thing. That's it - PHP has a lot of counting functions, as shown in the following link screenshot:
You don't need to master all of these. You will know that there are so many mathematical functions in PHP for you after reading them. That’s it. When needed in the future, just use it directly.
You only need to remember the most commonly used ones. I will give you a list of the most commonly used ones:
Function name | Description | Example | Input | Output |
---|---|---|---|---|
abs() | Request Absolute value | $abs = abs(-4.2); //4.2 | Number | Absolute value number |
ceil( ) | Round to an integer | echo ceil(9.999); // 10 | Floating point number | Round to an integer |
floor() | Truncation and rounding | echo floor(9.999); // 9 | Floating point number | Directly discard the decimal part |
fmod() | Floating point number remainder | "$x = 5.7;$y = 1.3;$r = fmod($x, $y);// $r equals 0.5, because 4 * 1.3 + 0.5 = 5.7 " | Two floating point numbers, x>y | Floating point remainder |
pow() | Returns the nth power of the number | echo pow(-1, 20); // 1 | Basic Number nth power | Power value |
round() | Floating point number rounding | echo round(1.95583, 2) ;// 1.96 | A numerical value | How many digits to keep after the decimal point, the default is 0. The rounded result |
sqrt() | Find the square root | echo sqrt(9); //3 | The number that is the square root | square root |
max() | Find the maximum value | "echo max(1, 3, 5, 6, 7); // 7 echo max(array(2, 4, 5)); / / 5" | Multiple numbers or arrays | Return the maximum value |
min() | Find the minimum value | min | Multiple numbers or arrays | Return the minimum value |
More Good random number | echo mt_rand(0,9);//n | Minimum/maximum, random number | Randomly returns a value within the range | |
Random number | echo rand() | Minimum/maximum, random number | Randomly returned within the range The value | |
Get the pi value | echo pi(); // 3.1415926535898 | None | Get pi |