PHP arithmetic operator
运算符 | 名称 | 描述 | 实例 | 结果 |
---|---|---|---|---|
x y | 加 | x 和 y 的和 | 2 2 | 4 |
x - y | 减 | x 和 y 的差 | 5 - 2 | 3 |
x * y | 乘 | x 和 y 的积 | 5 * 2 | 10 |
x / y | 除 | x 和 y 的商 | 15 / 5 | 3 |
x % y | 模(除法的余数) | x 除以 y 的余数 | 5 % 2 10 % 8 10 % 2 |
1 2 0 |
- x | 取反 | x 取反 | - 2 | |
a . b | 并置 | 连接两个字符串 | "Hi" . "Ha" | HiHa |
The following examples demonstrate different results obtained using different arithmetic operators:
<?php $x=10; $y=6; echo ($x + $y); // 输出16 echo ($x - $y); // 输出4 echo ($x * $y); // 输出60 echo ($x / $y); // 输出1.6666666666667 echo ($x % $y); // 输出4 ?>
Run
Related reading:
php strstr determines whether a string exists in another string
php stristr() function finds the first occurrence of a string in another string
php strchr() function finds the first occurrence of a string in another string
Detailed explanation of how to use php comparison function strncasecmp between two strings
php strcasecmp compares two strings for equality (compares the size of two strings)