无论如何,我可以知道哪种情况已经过去了。
if ( a || b ){
//do common operations to both
if (a){
//do a operation
}
if (b) {
//do be operation
}
}
感觉就像我在内心条件下做同样的事情。怎么可以避免这样做?有没有办法知道哪个条件通过并继续进行。有些事情..
if ( a || b ){
//do common operations to both
if first condition was passed{
//do a operation
}
if second condition was passed{
//do b operation
}
}
某些操作很昂贵,不必要地重复。感谢。
答案 0 :(得分:1)
如果在条件/表达式检查时指定值 。你可以很容易地知道哪个条件通过并继续进行。
$a = 5; //true
$b = 0; //false
if ( $a_t = $a || $b_t= $b ){
//do common operations to both
$both = $a+$b;
if ($a_t){
//do a operation
$a = $a+5;
}
if ($b_t) {
$b = $b+5;
//do be operation
}
}
echo "a=$a & b= $b & both=$both";
修改:它也适用于isset()
$a = 5; //true
$b = 0; //false
if ( $a_t = isset($a) || $b_t= isset($b) ){
//do common operations to both
$both = $a+$b;
if ($a_t){
//do a operation
$a = $a+5;
}
if ($b_t) {
$b = $b+5;
//do be operation
}
}
echo "a=$a & b= $b & both=$both";