Chapter 4 php mathematical operations

  • 2020-05-10 17:51:28
  • OfStack

1. Numerical data type
Numeric or numeric data in PHP 1 is usually of two types, double and int.
PHP is a loosely typed scripting language. Be aware of how the type is converted.
 
<?php 
$a = '5'; 
// A string of Numbers is also a number that participates in mathematical operations when Numbers are processed  
echo is_numeric ( $a ); //1 
echo '<br/>'; 
echo 7 + $a; //12 
echo '<br/>'; 
echo '7' + $a; //12 
echo '<br/>'; 
// with . The connection is processed as a string  
echo '7' . $a; //75 
?> 

Random number 2.
The Rand() function is a simple wrapper around a random function defined in libc.
The Mt_rand() function is a good alternative implementation.
 
<?php 
$a = rand(0,10); 
echo $a; 
echo '<br/>'; 
echo getrandmax(); 
echo '<br/>'; 
$b = mt_rand(0,10); 
echo $b; 
echo '<br/>'; 
echo mt_getrandmax(); 
echo '<br/>'; 
?> 

output
1
32767
6
2147483647
3. Format data
 
<?php 
$a = 12345.6789; 
// Used to set how many decimal places are reserved  
echo number_format($a,2); 
echo '<br/>'; 
// You can also change the notation for the default decimal point and the notation for the thousandths  
echo number_format($a,2,'#','*') 
?> 

Output
12,345.68
12*345#68
4. Mathematical function

function

function

Abs()

Take the absolute value

Floor()

Rounding off

Ceil()

Round by 1

Round()

4 5 in

Min()

To minimize or minimize in an array

Max()

To maximize or maximize in an array

 
<?php 
$a = -123456.789; 
$b = array (1, 2, 3, 4 ); 
echo abs ( $a ); 
echo '<br/>'; 
echo floor ( $a ); 
echo '<br>'; 
echo ceil ( $a ); 
echo '<br>'; 
echo round ( $a ); 
echo '<br>'; 
echo min ( $b ); 
echo '<br>'; 
echo max ( $b ); 
?> 

output
123456.789
-123457
-123456
-123457
1
4

Related articles: