-4

在 PHP 中,是否允许以下​​逻辑

If (x && y)
  //Do A
Elseif (x)
  // Do B
Elseif (y)
  // Do C
Else
  // Do D

基本上,您可以使用多个 elseif 吗?

4

5 回答 5

11

是的:

if ($x && $y) {
  //Do A
} else if ($x) {
  // Do B
} else if ($y) {
  // Do C
} else {
  // Do D
}

另一种对 HTML 文件有用的格式

<?php if ($x && $y): ?>
  Element A
<?php elseif ($x): ?>
  Element B
<?php elseif ($y): ?>
  Element C
<?php else: ?>
  Element D
<?php endif;?>
于 2009-08-15T01:15:19.060 回答
2

if(this == this && this == this){
   //this and that
}else if(this == that || this == that){
  //this or that
}else{
  //anything else
}
于 2009-08-15T01:15:12.897 回答
2

是的,尽管如果测试很简单($a == $b),请改用开关:

switch ($a) {
    case $b:
        break;
    case $c:
        break;
    default:
        //Like else
    }
于 2009-08-15T01:15:57.593 回答
2

是的。请参见http://us3.php.net/elseifhttp://us3.php.net/elseif

于 2009-08-15T01:18:03.287 回答
2

您可以使用

if($x)
    // for one line of code
elseif($y)
    // also for one line of code

if($x) {
    // for more than
    // one line of code
} elseif($y) {
    // also for multi-
    // line codes
}

if($x):
    // multi-line
endif;
于 2009-08-15T01:34:31.447 回答