Chapter 4 PHP Mathematical Operations_PHP Tutorial

WBOY
Release: 2016-07-21 15:21:47
Original
940 people have browsed it

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

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!