PHP 基础
PHP 简介
PHP 入门
PHP 语法
PHP 变量
PHP 常量
PHP 输出和打印
PHP 数据类型
PHP 字符串
PHP 运算符
PHP If…Else
PHP Switch…Case
PHP 数组
PHP 数组排序
PHP 循环
PHP 函数
PHP 数学运算
PHP GET 和 POST
PHP 高级
PHP 日期和时间
PHP 包含文件
PHP 文件系统
PHP 解析目录
PHP 文件上传
PHP 文件下载
PHP Cookies
PHP Sessions
PHP 发送邮件
PHP 表单处理
PHP 表单验证
PHP 过滤器
PHP 错误处理
PHP 类和对象
PHP 魔术常量
PHP JSON 解析
PHP 正则表达式
PHP 异常处理
PHP 和 MySQL 数据库
PHP MySQL 简介
PHP MySQL 连接
PHP MySQL Create Database
PHP MySQL Create Table
PHP MySQL Insert
PHP MySQL Prepared
PHP MySQL Last Inserted ID
PHP MySQL Select
PHP MySQL Where
PHP MySQL Limit
PHP MySQL Order By
PHP MySQL Update
PHP MySQL Delete
PHP MySQL CRUD 应用
PHP MySQL Ajax 搜索
PHP MySQL 登录系统
PHP参考
PHP String Functions
PHP Array Functions
PHP File System Functions
PHP Date/Time Functions
PHP Calendar Functions
PHP MySQLi Functions
PHP Filters
PHP Error Levels
PHP常见问题解答
如何在 PHP 中编写注释
如何在 PHP 中删除字符串中的空格
如何在 PHP 中查找字符串中的字符数
如何在 PHP 中查找字符串中的单词数
如何在 PHP 中删除字符串中的特殊字符
如何在 PHP 中替换字符串中的一个单词
如何在 PHP 中对字符串前面追加
如何在 PHP 中对字符串后面追加
如何在 PHP 中从字符串中提取子串
如何在 PHP 中比较两个字符串
如何在 PHP 中获取当前页面的 URL
如何在 PHP 中通过连接数组值创建字符串
如何在 PHP 中将字符串拆分为数组
如何在 PHP 中合并两个字符串
如何在 PHP 中把字符串转换成小写字母
如何在 PHP 中把字符串转换成大写字母
如何在 PHP 中把字符串的第一个字母转换成大写字母
如何在 PHP 中把特殊的 HTML 实体转换回字符
如何在 PHP 中删除字符串开头的空格
如何在 PHP 中删除字符串结尾的空格
如何在 PHP 中新建一行
如何在 PHP 中查找字符串长度
如何在 PHP 中检查变量是否已设置
如何在 PHP 中检查变量是否为空
如何在 PHP 中检查变量是否为NULL
如何在 PHP 中反转字符串
如何在 PHP 中用另一个字符串替换字符串的一部分
如何在 PHP 中计算子串在字符串中出现的次数
如何在 PHP 中计算数组中的所有元素
如何在 PHP 中打印或回显数组的所有值
如何在 PHP 中显示数组的结构和值
如何在 PHP 中颠倒数组的顺序
如何在 PHP 中检查数组中是否存在值
如何在 PHP 中检查数组中是否存在键
如何在 PHP 中删除数组中的最后一个元素
如何从 PHP 数组中删除第一个元素
如何在 PHP 中为数组的开头添加元素
如何在 PHP 中为数组的末尾添加元素
如何在 PHP 中把两个或多个数组合并成一个数组
如何在 PHP 中按字母顺序对数组值排序
如何在 PHP 中删除数组中的重复值
如何在 PHP 中随机调整数组的顺序
如何在 PHP 中比较两个数组的值
如何在 PHP 中计算数组中数值的总和
如何在 PHP 中删除数组中的空值
如何在 PHP 中用数组值填充下拉列表
如何在 PHP 中获取关联数组的所有键值
如何在 PHP 中获取关联数组的所有值
如何在 PHP 中按键对关联数组排序
如何在 PHP 中按值对关联数组排序
如何在 PHP 中从数组中获取单个值
如何在 PHP 中循环浏览多维数组
如何在 PHP 中从数组中删除元素
如何在 PHP 中检查字符串是否包含特定单词
如何在 PHP 中获取当前日期和时间
如何在 PHP 中进行重定向
如何在 PHP 中删除字符串中的所有空格
如何用 PHP 获取当前年份
如何在 PHP 中将日期从 yyyy-mm-dd 转换为 dd-mm-yyyy 格式
如何在 PHP 中将字符串转换为数字
如何在 PHP 中获取数组的第一个元素
如何在 PHP 中将日期转换为时间戳
如何在 PHP 中为空数组添加元素
如何在 PHP 中把整数转换成字符串
如何用值而不是键删除 PHP 数组元素
如何在 PHP 中将键和值同时推入数组
如何使用 PHP 定期刷新页面
如何从 PHP 字符串中删除最后一个字符
如何从 PHP 脚本返回 JSON
如何让 PHP 显示错误
PHP 数学运算 - php7基础教程 - 笔下光年
网站首页
PHP 数学运算
In this tutorial you will learn how to perform mathematical operations in PHP. ## Performing Math Operations PHP has several built-in functions that help you perform anything from simple additions or subtraction to advanced calculations. You've already seen how to perform basic mathematical operations in [PHP operators](http://www.bixiaguangnian.com/manual/php7/3971.html "PHP operators") chapter. Let's check out one more example: ```php <?php echo 7 + 3; // 0utputs: 10 echo 7 - 2; // 0utputs: 5 echo 7 * 2; // 0utputs: 14 echo 7 / 2; // 0utputs: 3.5 echo 7 % 2; // 0utputs: 1 ?> ``` Every math operation has a certain precedence level; generally multiplication and division are performed before addition and subtraction. However, parentheses can alter this precedence; expressions enclosed within parentheses are always evaluated first, regardless of the operation's precedence level, as demonstrated in the following example: ```php <?php echo 5 + 4 * 10; // 0utputs: 45 echo (5 + 4) * 10; // 0utputs: 90 echo 5 + 4 * 10 / 2; // 0utputs: 25 echo 8 * 10 / 4 - 2; // 0utputs: 18 echo 8 * 10 / (4 - 2); // 0utputs: 40 echo 8 + 10 / 4 - 2; // 0utputs: 8.5 echo (8 + 10) / (4 - 2); // 0utputs: 9 ?> ``` In the following section we're going to look at some built-in PHP functions that are most frequently used in performing mathematical operations. ## Find the Absolute Value of a Number The absolute value of an [integer](http://www.bixiaguangnian.com/manual/php7/3969.html#h2-php-integers "integer") or a [float](http://www.bixiaguangnian.com/manual/php7/3969.html#h2-php-floating-point-numbers-or-doubles "float") can be found with the abs() function, as demonstrated in the following example: ```php <?php echo abs(5); // 0utputs: 5 (integer) echo abs(-5); // 0utputs: 5 (integer) echo abs(4.2); // 0utputs: 4.2 (double/float) echo abs(-4.2); // 0utputs: 4.2 (double/float) ?> ``` As you can see if the given number is negative, the valued returned is positive. But, if the number is positive, this function simply returns the number. ## Round a Fractional Value Up or Down The `ceil()` function can be used to round a fractional value up to the next highest integer value, whereas the `floor()` function can be used to round a fractional value down to the next lowest integer value, as demonstrated in the following example: ```php <?php // Round fractions up echo ceil(4.2); // 0utputs: 5 echo ceil(9.99); // 0utputs: 10 echo ceil(-5.18); // 0utputs: -5 // Round fractions down echo floor(4.2); // 0utputs: 4 echo floor(9.99); // 0utputs: 9 echo floor(-5.18); // 0utputs: -6 ?> ``` ## Find the Square Root of a Number You can use the `sqrt()` function to find the square root of a positive number. This function returns a special value `NAN` for negative numbers. Here's an example: ```php <?php echo sqrt(9); // 0utputs: 3 echo sqrt(25); // 0utputs: 5 echo sqrt(10); // 0utputs: 3.1622776601684 echo sqrt(-16); // 0utputs: NAN ?> ``` ## Generate a Random Number The `rand()` function can be used to generate a random number. You can optionally specify a range by passing the min, max arguments, as shown in the following example: ```php <?php // Generate some random numbers echo rand() . "<br>"; echo rand() . "<br>"; // Generate some random numbers between 1 and 10 (inclusive) echo rand(1, 10) . "<br>"; echo rand(1, 10) . "<br>"; ?> ``` If `rand()` function is called without the optional min, max arguments, it returns a pseudo-random number between `0` and `getrandmax()`. The `getrandmax()` function show the largest possible random value, which is only 32767 on Windows platform. So, if you require a range larger than 32767, you may simply specify the min and max arguments. ## Convert Decimal Numbers to Binary and Vice Versa The `decbin()` function is used to convert a decimal number into binary number. Whereas its counterpart the `bindec()` function converts a number from binary to decimal. ```php <?php // Convert Decimal to Binary echo decbin(2); // 0utputs: 10 echo decbin(12); // 0utputs: 1100 echo decbin(100); // 0utputs: 1100100 // Convert Binary to Decimal echo bindec(10); // 0utputs: 2 echo bindec(1100); // 0utputs: 12 echo bindec(1100100); // 0utputs: 100 ?> ``` ## Convert Decimal Numbers to Hexadecimal and Vice Versa The `dechex()` function is used to convert a decimal number into hexadecimal representation. Whereas, the `hexdec()` function is used to converts a hexadecimal string to a decimal number. ```php <?php // Convert decimal to hexadecimal echo dechex(255); // 0utputs: ff echo dechex(196); // 0utputs: c4 echo dechex(0); // 0utputs: 0 // Convert hexadecimal to decimal echo hexdec('ff'); // 0utputs: 255 echo hexdec('c4'); // 0utputs: 196 echo hexdec(0); // 0utputs: 0 ?> ``` ## Convert Decimal Numbers to Octal and Vice Versa The `decoct()` function is used to convert a decimal number into octal representation. Whereas, the `octdec()` function is used to converts a octal number to a decimal number. ```php <?php // Convert decimal to octal echo decoct(12); // 0utputs: 14 echo decoct(256); // 0utputs: 400 echo decoct(77); // 0utputs: 115 // Convert octal to decimal echo octdec('14'); // 0utputs: 12 echo octdec('400'); // 0utputs: 256 echo octdec('115'); // 0utputs: 77 ?> ``` ## Convert a Number from One Base System to Another The `base_convert()` function can be used to convert a number from one base system to other. For example, you can convert decimal (base 10) to binary (base 2), hexadecimal (base 16) to octal (base 8), octal to hexadecimal, hexadecimal to decimal, and so on. This function accepts three parameters: the number to convert, the base it's currently in, and the base it's to be converted to. The basic syntax is as follows: ```php base_convert(number, frombase, tobase); ``` Here, the number can be either an integer or a string representing an integer. Both frombase and tobase have to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented with the letters a-z, where a means 10, b means 11 and z means 35. Here's a simple example to show how this function works: ```php <?php // Convert decimal to binary echo base_convert('12', 10, 2); // 0utputs: 1100 // Convert binary to decimal echo base_convert('1100', 2, 10); // 0utputs: 12 // Convert decimal to hexadecimal echo base_convert('10889592', 10, 16); // 0utputs: a62978 // Convert hexadecimal to decimal echo base_convert('a62978', 16, 10); // 0utputs: 10889592 // Convert decimal to octal echo base_convert('82', 10, 8); // 0utputs: 122 // Convert octal to decimal echo base_convert('122', 8, 10); // 0utputs: 82 // Convert hexadecimal to octal echo base_convert('c2c6a8', 16, 8); // 0utputs: 60543250 // Convert octal to hexadecimal echo base_convert('60543250', 8, 16); // 0utputs: c2c6a8 // Convert octal to binary echo base_convert('42', 8, 2); // 0utputs: 100010 // Convert binary to octal echo base_convert('100010', 2, 8); // 0utputs: 42 // Convert hexadecimal to binary echo base_convert('abc', 16, 2); // 0utputs: 101010111100 // Convert binary to hexadecimal echo base_convert('101010111100', 2, 16); // 0utputs: abc ?> ``` <div class="callout callout-info mb-3">Note: The base_convert() function will always return a string value. If the returned value is in base 10 the resulting string can be used as a numeric string in calculations and PHP will convert it to a number when the calculation is performed.</div>
上一篇:
PHP 函数
下一篇:
PHP GET 和 POST