1. Numerical data types
There are generally two types of numerical or numerical data in PHP: double and int.
PHP is a loosely typed scripting language, so pay attention to the method of type conversion.
Copy code The code is as follows:
$a = '5';
/ / Strings of numbers are also numbers and participate in mathematical operations when number processing
echo is_numeric ( $a ); //1
echo '
';
echo 7 + $a; / /12
echo '
';
echo '7' + $a; //12
echo '
';
//Connect with . Then process it as a string
echo '7' . $a; //75
?>
2. Random number
Rand() function is defined in libc A simple wrapper for a random function.
Mt_rand() function is a good alternative implementation.
Copy code The code is as follows:
$a = rand(0,10);
echo $a;
echo '
';
echo getrandmax();
echo '
';
$b = mt_rand(0, 10);
echo $b;
echo '
';
echo mt_getrandmax();
echo '
';
?>
output
1
32767
6
2147483647
3. Format data
Copy codeThe code is as follows:
$a = 12345.6789;
//Used to set how many decimal places to retain
echo number_format($a,2 );
echo '
';
//You can also change the default decimal point symbol and thousandths symbol
echo number_format($a,2,'#', '*')
?>
Output
12,345.68
12*345#68
IV. Math functions
函数
|
功能
|
Abs()
|
取绝对值
|
Floor()
|
舍去法取整
|
Ceil()
|
进一法取整
|
Round()
|
四舍五入
|
Min()
|
求最小值或数组中最小值
|
Max()
|
求最大值或数组中最大值
|
Copy code The code is as follows:
$a = -123456.789;
$b = array (1, 2, 3, 4);
echo abs ( $a );
echo '
';
echo floor ( $a );
echo '
';
echo ceil ( $a );
echo '
';
echo round ( $a );
echo '
';
echo min ( $b );
echo '
';
echo max ( $b );
?>
output
123456.789
-123457
-123456
-123457
1
4
http://www.bkjia.com/PHPjc/324809.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324809.htmlTechArticle1. Numerical data types There are generally two types of numerical or numerical data in PHP: double and int. PHP is a loosely typed scripting language, so pay attention to the method of type conversion. Copy the code The code is as follows...