Home  >  Article  >  Backend Development  >  PHP基础之运算符的使用方法_php实例

PHP基础之运算符的使用方法_php实例

WBOY
WBOYOriginal
2016-06-07 17:24:44764browse

1、算术运算符:+、-、*、/、%。

2、递增/递减运算符:如$a++,$a--,++$a,--$a.

如:

$a=10;
$b=5;
$c=$a++; //先赋值,后自增。$c=$a,$a=$a+1
$d=$b--; //先赋值,后自减。$d=$b,$b=$a-1
echo '$a='.$a."||".'$c='.$c.'
'; //$a=11,$c=10
echo '$b='.$b."||".'$d='.$d.'
'; //$b=4,$d=5
?>

$a=10;
$b=5;
$c=++$a; //先自增,后赋值。$a=$a+1,$c=$a
$d=--$b; //先自减,后赋值。$b=$a-1, $d=$b
echo '$a='.$a."||".'$c='.$c.'
'; //$a=11,$c=11
echo '$b='.$b."||".'$d='.$d.'
'; //$b=4,$d=4
?>

3、比较算符:参考文档

4、逻辑运算符:

如:

$a=10;$b=7;
if($a++>8 || $b++>7){ //$a++>8为真,$b++>7这个就不执行了
echo 'OK!';
}
echo 'a='.$a.'b='.$b; // 输出OK,a=11,b=7

改变一下

$a=10;$b=7;
if($a++>10 && $b++>7){ //$a++>8为假,$b++>7这个就不执行了
echo 'OK!';
}
echo 'a='.$a.'b='.$b; // a=11,b=7

细节:and  && 都表示逻辑与 ,他们的区别在什么地方?

主要体现在优先级上面

and 的优先级
and
or
如:

$a=false || true; //&& > = > and ;先比较false || true,再赋值
$b=false or true; //|| > = > or  ;先赋值$b=false,再比较,所以结果是false
var_dump($a,$b); //bool(true) bool(false)

Statement:
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