php if else if语句

时间:2016-10-27 13:24:53

标签: php algorithm if-statement

我正在尝试输出上面的变量以输出html,所以我可以用来报告我的html表的一列中的状态。我有以下代码,但它似乎没有工作,因为我不断得到“未预见到”的错误。

<?php
 $st = $row_all_students['mem_review_stat'];


  if ($st == "Pending") {
      echo "<span class="label label-info">Pending</span>";
  } elseif ($st == "on hold") {
      echo "<span class="label label-danger">Suspended</span>";
  } else {
      echo "<span class="label label-success">Reviewed</span>";
}
?>

有人可以用正确的语法帮助我吗? 提前谢谢!

3 个答案:

答案 0 :(得分:1)

试试这段代码:

<?php
 $st = $row_all_students['mem_review_stat'];


  if ($st == "Pending") {
      echo '<span class="label label-info">Pending</span>';
  } elseif ($st == "on hold") {
      echo '<span class="label label-danger">Suspended</span>';
  } else {
      echo '<span class="label label-success">Reviewed</span>';
}
?>

答案 1 :(得分:1)

你必须逃避你的内心"所以PHP知道它们是字符串的一部分,而不是它的结尾。

<?php
 $st = $row_all_students['mem_review_stat'];


  if ($st == "Pending") {
      echo "<span class=\"label label-info\">Pending</span>";
  } elseif ($st == "on hold") {
      echo "<span class=\"label label-danger\">Suspended</span>";
  } else {
      echo "<span class=\"label label-success\">Reviewed</span>";
}
?>

或者:使用单引号:

<?php
 $st = $row_all_students['mem_review_stat'];


  if ($st == "Pending") {
      echo '<span class="label label-info">Pending</span>';
  } elseif ($st == "on hold") {
      echo '<span class="label label-danger">Suspended</span>';
  } else {
      echo '<span class="label label-success">Reviewed</span>';
}
?>

答案 2 :(得分:1)

你必须逃避报价。 有几种方法。有关详细信息,请查看以下答案:https://stackoverflow.com/a/7999163/3119231

<?php
$st = $row_all_students['mem_review_stat'];
if ($st == "Pending") echo "<span class='label label-info'>Pending</span>";
elseif ($st == "on hold") echo "<span class='label label-danger'>Suspended</span>";
else echo "<span class='label label-success'>Reviewed</span>";
?>