前端学PHP之运算符,学PHP之运算符
目录
算术运算符 赋值运算符 比较运算符 三元运算符 逻辑运算符 字符串运算符 错误控制运算符 递增递减运算符 数组运算符
定义
运算符即做运算的标识符号。PHP运算符一般分为算术运算符、赋值运算符、比较运算符、三元运算符、逻辑运算符、字符串连接运算符、错误控制运算符、递增递减运算符和数组运算符
算术运算符
1 2 3 4 5 6 7 8 9 10 | +<span>(加法)
</span><span> $x </span> + <span> $y </span>
-<span>(减法)
</span><span> $x </span> - <span> $y </span>
*<span>(乘法)
</span><span> $x </span> * <span> $y </span>
/<span>(除法)
</span><span> $x </span> / <span> $y </span>
%<span>(求模)
</span><span> $x </span> % <span> $y </span>
|
로그인 후 복사
赋值运算符
PHP的赋值运算符有两种,分别是直接赋值"="和引用赋值"&"
[1]直接赋值
直接赋值"="把右边表达式的值赋给左边的运算数。它将右边表达式值复制一份,交给左边运算数。换言之,首先给左边运算数申请一块内存,然后把复制的值放到这块内存中
1 2 3 4 5 6 | x =<span> y
x </span>+=<span> y
x </span>-=<span> y
x </span>*=<span> y
x </span>/=<span> y
x </span>%= y
|
로그인 후 복사
[2]引用赋值
引用赋值&意味着两个变量都指向同一个数据。它将使两个变量共享一块内存,如果这个内存存储的数据变了,那么两个变量的值都会发生变化
1 2 3 4 5 6 7 8 | <?<span>php
</span><span> $a </span> = "测试内容1" <span>;
</span><span> $b </span> = <span> $a </span><span>;
</span><span> $c </span> = &<span> $a </span><span>;
</span><span> $a </span> = "测试内容2" <span>;
</span><span> echo </span> <span> $b </span>. "<br />" ;<span>
<span> echo </span> <span> $c </span>. "<br />" ;<span>
?>
|
로그인 후 복사
比较运算符
比较运算符主要用来进行比较运算
1 2 3 4 5 6 7 8 9 | ==<span> 等于
</span>===<span> 全等
</span>!=<span> 不等
</span><><span> 不等
</span>!==<span> 不全等
</span>><span> 大于
</span><<span> 小于
</span>>=<span> 大于等于
</span><= 小于等于
|
로그인 후 복사
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?<span>php
</span><span> $a </span> = 1<span>;
</span><span> $b </span> = "1" <span>;
</span><span>var_dump</span>(<span> $a </span> == <span> $b </span><span>);
</span><span> echo </span> "<br />" ;<span>
<span>var_dump</span>(<span> $a </span> === <span> $b </span><span>);
</span><span> echo </span> "<br />" ;<span>
<span>var_dump</span>(<span> $a </span> != <span> $b </span><span>);
</span><span> echo </span> "<br />" ;<span>
<span>var_dump</span>(<span> $a </span> <> <span> $b </span><span>);
</span><span> echo </span> "<br />" ;<span>
<span>var_dump</span>(<span> $a </span> !== <span> $b </span><span>);
</span><span> echo </span> "<br />" ;<span>
<span>var_dump</span>(<span> $a </span> < <span> $b </span><span>);
</span><span> echo </span> "<br />" ;<span>
?>
|
로그인 후 복사
三元运算符
"?:"三元运算符是一个比较运算符,对于表达式(expr1)?(expr2):(expr3),如果expr1的值为true,则此表达式的值为expr2,否则为expr3
1 2 3 4 5 | <?<span>php
</span><span> $a </span> = 78;<span>
<span> $b </span> = <span> $a </span> >=60? "及格" : "不及格" <span>;
</span><span> echo </span> <span> $b </span>;<span>
?>
|
로그인 후 복사
逻辑运算符
逻辑运算符主要是进行逻辑运算
1 2 3 4 5 6 | <span> and 与
or 或
xor 异或
</span>&&<span> 与
</span>||<span> 或
</span>! 非
|
로그인 후 복사
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?<span>php
</span><span> $a </span> = <span>TRUE</span>; <span>
<span> $b </span> = <span>TRUE</span>; <span>
<span> $c </span> = <span>FALSE</span>; <span>
<span> $d </span> = <span>FALSE</span>; <span>
<span> echo </span> (<span> $a </span> and <span> $b </span>);<span>
<span> echo </span> "<br />" <span>;
</span><span> echo </span> (<span> $a </span> or <span> $c </span>);<span>
<span> echo </span> "<br />" <span>;
</span><span> echo </span>(<span> $a </span> xor <span> $c </span> xor <span> $d </span>);<span>
<span> echo </span> "<br />" <span>;
</span><span> echo </span>(!<span> $c </span> ? "通过" : "不通过" );<span>
<span> echo </span> "<br />" <span>;
</span><span> echo </span>(<span> $a </span> && <span> $d </span> ? "通过" : "不通过" );<span>
<span> echo </span> "<br />" <span>;
</span><span> echo </span> (<span> $b </span> || <span> $c </span> || <span> $d </span> ? "通过" : "不通过" );<span>
?>
|
로그인 후 복사
字符串运算符
字符串连接运算符是为了将两个字符串进行连接
[1]连接运算符(.)
1 2 3 4 5 6 | <?<span>php
</span><span> $a </span> = 'hello' <span>;
</span><span> $b </span> = <span> $a </span> . ' world!' <span>;
</span><span>
<span> echo </span> <span> $b </span><span>;
</span>?>
|
로그인 후 복사
[2]连接赋值运算符(.=)
1 2 3 4 5 6 | <?<span>php
</span><span> $x </span> = 'hello' <span>;
</span><span> $x </span> .= ' world!' <span>;
</span><span>
<span> echo </span> <span> $x </span><span>;
</span>?>
|
로그인 후 복사
错误控制运算符
PHP提供了一个错误控制运算符@,对于一些可能会有运行过程中出错的表达式,不希望出错时显示错误信息时,将@放置在一个PHP表达式之前。如果激活了track_error特性,表达式产生的任何错误信息都被存放在变量$php_errormsg中,此变量在每次出错时都会被覆盖
[注意]错误控制前缀@不会屏蔽解析错误的信息,不能把它放在函数或类的定义之前,也不能用于条件结构如if和foreach等
1 2 3 4 5 6 | <?<span>php
</span><span> $a </span> = 1<span>;
</span><span> echo </span> @ <span> $a </span>;<span>
<span> $b </span><span>;
</span><span> echo </span> @ <span> $b </span>;<span>
?>
|
로그인 후 복사
递增/递减运算符
1 2 3 4 | ++<span> $x </span><span> 前递增
</span><span> $x </span>++<span> 后递增
</span>--<span> $x </span><span> 前递减
</span><span> $x </span>-- 后递减
|
로그인 후 복사
1 2 3 4 5 6 7 8 9 10 | <?<span>php
</span><span> $x </span>=10<span>;
</span><span> echo </span> ++<span> $x </span>; <span>
<span> $y </span>=10<span>;
</span><span> echo </span> <span> $y </span>++; <span>
<span> $z </span>=5<span>;
</span><span> echo </span> --<span> $z </span>; <span>
<span> $i </span>=5<span>;
</span><span> echo </span> <span> $i </span>--; <span>
?>
|
로그인 후 복사
数组运算符
用于比较数组
1 2 3 4 5 6 | +<span> 联合
</span>==<span> 相等
</span>===<span> 全等
</span>!=<span> 不相等
</span><><span> 不相等
</span>!== 不全等
|
로그인 후 복사
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?<span>php
</span><span> $x </span> = <span> array </span>( "a" => "red" , "b" => "green" <span>);
</span><span> $y </span> = <span> array </span>( "c" => "blue" , "d" => "yellow" <span>);
</span><span> $z </span> = <span> $x </span> + <span> $y </span><span>;
</span><span>var_dump</span>(<span> $z </span>);<span>
<span> echo </span> "<br>" <span>;
</span><span>var_dump</span>(<span> $x </span> == <span> $y </span>);<span>
<span> echo </span> "<br>" <span>;
</span><span>var_dump</span>(<span> $x </span> === <span> $y </span>);<span>
<span> echo </span> "<br>" <span>;
</span><span>var_dump</span>(<span> $x </span> != <span> $y </span>);<span>
<span> echo </span> "<br>" <span>;
</span><span>var_dump</span>(<span> $x </span> <> <span> $y </span>);<span>
<span> echo </span> "<br>" <span>;
</span><span>var_dump</span>(<span> $x </span> !== <span> $y </span>);<span>
?>
|
로그인 후 복사