逻辑else/if不工作


Logical else/if not working

我在PHP中逻辑else ' if '有问题。这是我的菜单:

?hal=daftar
?hal=Visi
?hal=berita

For my page:

if(isset($_GET['hal'])=='daftar'){
      include"daftar.php";
  }
  elseif(isset($_GET["hal"])=="Visi"){
      include"profil.php";
  }
  else if(isset($_GET['hal'])=='berita'){
      include"berita.php";
  }
  else
  {
echo"wrong"
}

问题是为什么它只出现在daftar页,或者如果我点击菜单?hal=Visi, ?hal=berita总是出现在daftar页。

isset只返回一个布尔值truefalse,所以它永远不会匹配truefalse以外的值。要将isset与比较运算符一起使用,请这样做:

if (isset($_GET['hal']) && $_GET["hal"] == 'daftar'){
  include "daftar.php";
}
else if (isset($_GET["hal"]) && $_GET["hal"] == "Visi"){
  include "profil.php";
}
else if (isset($_GET['hal']) && $_GET["hal"] == 'berita'){
  include "berita.php";
}
else {
  echo "wrong"
}

可以正常运行:

$myvar = $_GET['hal'];
if($myvar =='daftar'){
      include"daftar.php";
  }
  else if($myvar =="Visi"){
      include"profil.php";
  }
  else if($myvar =='berita'){
      include"berita.php";
  }
  else
  {
echo"wrong"
}

您在每个条件中将bool值与字符串进行比较,因为isset()返回布尔值