php之and,or使用
2017-10-30 本文已影响0人
奋斗live
php的and,or,其实有着then,else的作用,看如下例子
1
<?php
$b = 'bbbbb';
$a = 'ggg';
$a and $c=$a;
var_dump($c);die;
?>
与下面例子是等价的
<?php
$b = 'bbbbb';
$a = 'ggg';
if($a){
$c = $a;
}
var_dump($c);die;
?>
2
<?php
$b = 'bbbbb';
$a = '';
$c = '';
$c and $d=$a or $d=$b;
?>
与下面例子是等价的
if($c){
$d = $a;
}else{
$d = $b;
}
var_dump($d);
3
<?php
$b = 'bbbbb';
$a = '';
$c = '';
$a and ($c and $d=$c or $d='no');
?>
与下面例子是等价的
if($a){
if($c){
$d=$c;
}else{
$d='no';
}
}