Summary of Common Mathematical Functions in php

  • 2021-08-05 09:29:28
  • OfStack

This paper summarizes and analyzes the common mathematical functions of php with examples. Share it for your reference. The specific analysis is as follows:

abs () function definition and usage: Returns the absolute value of 1 number.

Syntax: abs (x), code as follows:

$abs=abs(-3.2);      //$abs=3.2 
$abs2=abs(5);       //$abs2=5
$abs3=abs(-5);       //$abs3=5

ceil () function definition and usage: Rounding up to the nearest integer.

Syntax ceil (x)

参数 描述
x 必需,1个数.

Note: Return not less than x next 1 integer, x if there is a decimal part, ceil () return type is still float, because float value range is usually larger than integer, example code is as follows:

echo ceil(5);         //5 
echo "<br>";
echo ceil(3.3);         //4
echo "<br>";
echo ceil(6.999);        //7

The floor () function is rounded down to the nearest integer.

Syntax: floor (x)

参数 描述
x 必需,1个数.

Note: Returns the next integer not greater than x, rounding the decimal part of x, floor () returns the type float, because the range of float values is usually larger than integer.

echo floor(4);        //4 
echo "<br>";
echo floor(3.3);        //3
echo "<br>";
echo floor(6.999);       //6

Definition and usage

The fmod () function returns the floating-point remainder of the division.

Syntax: fmod (x, y)

参数 描述
x 必需,1个数.
y 必需,1个数.

Description: Returns the floating-point remainder of the dividend (x) divided by the divisor (y). The remainder (r) is defined as: x = i * y + r, where i is an integer. If y is a non-zero value, r and x have the same sign and the number value is less than y, with the following code:

$x=4.7;            // Definition value 1 
$y=1.3;            // Definition value 2
$r=fmod($x,$y);          // Perform the remainder operation
echo $x." Divide by ".$y." The floating-point remainder of is: ".$r;   // Output result

The logarithm of log10 () based on 10.

Syntax: log10 (x)

参数 描述
x 必需,1个数.

Description: Returns the bottom 10 logarithm of the parameter x with the following code:

$num1=100; 
$num2=1000;
$num3=3;
$result1=log10($num1);
$result2=log10($num2);
$result3=log10($num3);
echo "$num1 With 10 The base logarithm is $result1";
echo "<br>";
echo "$num2 With 10 The base logarithm is $result2";
echo "<br>";
echo "$num3 With 10 The base logarithm is $result3";
echo "<br>";

log () returns the natural logarithm.

Syntax: log (x, base)

参数 描述
x 必需,1个数.

base is optional, and if this parameter is specified, logbasex is returned.

Note: If the optional parameter base is specified, log () returns logbasex, otherwise log () returns the natural logarithm of the parameter x. The example code is as follows:

echo log(2.7183);       // Returns the natural logarithm of the specified value  
echo "<br/>";
echo log(2);        // Returns the natural logarithm of the specified value
echo "<br/>";
echo log(1);        // Returns the natural logarithm of the specified value
echo "<br/>";
echo log(0);        // Returns the natural logarithm of the specified value
echo "<br/>";
echo log(-1);        // Returns the natural logarithm of the specified value
//
echo sqrt(9);         // Output 3
echo "<br>";
echo sqrt(10);         //3.16227766...
//
var_dump(pow(2, 8));       // Output 256
echo "<br>";
echo pow(-1,20);        // Output 1
echo "<br>";
echo pow(0,0);         // Output 1
echo "<br>";
echo pow(-1, 4.5);        // Return error

I hope this article is helpful to everyone's PHP programming.


Related articles: