Home > php教程 > php手册 > body text

php中运算符基本介绍

WBOY
Release: 2016-06-13 10:15:43
Original
1035 people have browsed it

在php中我们常用的运算符包括有算数运算符、赋值运算符、比较运算符、逻辑运算符 等等下面我来给各位朋友介绍用法。

算数运算符

除法运算符总是返回浮点数。只有在下列情况例外:两个操作数都是整数(或字符串转换成的整数)并且正好能整除,这时它返回一个整数。

取模运算符的操作数在运算之前都会转换成整数(除去小数部分)。

Note: 取模 $a % $b 在 $a 为负值时的结果也是负值。

例:

 代码如下 复制代码

/* tested under PHP 5.2.6-1 with Suhosin-Patch 0.9.6.2 (cli) on both i386 and amd64, Debian lenny/sid */
$a = 2863311530;
$b = 256;
$c = $a % $b;
echo "$c
n";
echo (2863311530 % 256)."
n"; /* directly with no variables, just to be sure */
?>

运算符 说明 例子 结果
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
-- Decrement x=5
x--
x=4

赋值运算符

基本的赋值运算符是“=”。一开始可能会以为它是“等于”,其实不是的。它实际上意味着把右边表达式的值赋给左边的运算数。

赋值运算表达式的值也就是所赋的值。也就是说,“$a = 3”的值是 3。这样就可以做一些小技巧:

 代码如下 复制代码

$a = ($b = 4) + 5; // $a 现在成了 9,而 $b 成了 4。

?>

在基本赋值运算符之外,还有适合于所有二元算术,数组集合和字符串运算符的“组合运算符”,这样可以在一个表达式中使用它的值并把表达式的结果赋给它,例如:

 代码如下 复制代码

$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";

?>

注意赋值运算将原变量的值拷贝到新变量中(传值赋值),所以改变其中一个并不影响另一个。这也适合于在很密集的循环中拷贝一些值例如大数组。也可以使用引用赋值,用 $var = &$othervar; 语法。引用赋值意味着两个变量都指向同一个数据,没有任何数据的拷贝。有关引用的更多信息见引用的说明。在 PHP 5中,对象总是通过引用赋值的,除非明确使用新的 clone关键字

运算符 说明 例子
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

比较运算符

比较运算符,如同它们名称所暗示的,允许对两个值进行比较。如果比较一个整数和字符串,则字符串会被转换为整数。如果比较两个数字字符串,则作为整数比较。此规则也适用于 switch 语句。

 代码如下 复制代码

var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("1" == "1e0"); // 1 == 1 -> true
switch ("a") {
case 0:
    echo "0";
    break;
case "a": // never reached because "a" is already matched with 0
    echo "a";
    break;
}
?>

运算符 说明 例子
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
is less than 5
>= is greater than or equal to 5>=8 returns false
is less than or equal to 5

逻辑运算符

“与”和“或”有两种不同形式运算符的原因是它们运算的优先级不同(见运算符优先级)。

Example #1 逻辑运算符示例

 代码如下 复制代码

// 下面的 foo() 不会被调用,因为它们被运算符“短路”了。
$a = (false && foo());
$b = (true  || foo());
$c = (false and foo());
$d = (true  or  foo());

// "||" 的优先级比 "or" 高
$e = false || true; // $e 被赋值为 (false || true),结果为 true
$f = false or true; // $f 被赋值为 false [Altair注:"=" 的优先级比 "or" 高]
var_dump($e, $f);

// "&&" 的优先级比 "and" 高
$g = true && false; // $g 被赋值为 (true && false),结果为 false
$h = true and false; // $h 被赋值为 true [Altair注:"=" 的优先级比 "and" 高]
var_dump($g, $h);
?>
以上例程的输出类似于:

bool(true)
bool(false)
bool(false)
bool(true)

运算符 说明 例子
&& and x=6
y=3

(x 1) returns true

|| or x=6
y=3

(x==5 || y==5) returns false

! not x=6
y=3

!(x==y) returns true

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!