猿问

如果在 PHP 中使用 else 程序,需要有关此方面的帮助

<HTML>

  <body>


<?php

echo "<H4>Are you a Male</h4>"

 ?>

<form action ="ifstat.php" method ="post">

  Enter Please: <input type="text" name="val1">

  <input type ="submit" >

  <input type ="reset">

</form>


<?php

  $isMale=$_POST["val1"];

if($isMale =="y" || "yes"){

  echo("You are a Male");

} elseif($isMale =="n" || "no") {

  echo("You are a female");

} else{

  echo("Invalid entry");

}


?>

</body>

</html>

所以,这是我的程序代码。即使我输入“n”或其他值而不是 y,我也总是不断地将 You are a Male 作为输出。这里的代码有什么问题?


这是输出的照片https://imgur.com/a/PPH49GK


慕码人2483693
浏览 86回答 2
2回答

翻翻过去那场雪

您需要将条件格式从更改if($isMale =="y" || "yes"){为if($isMale =="y" || $isMale =="yes"){<HTML>&nbsp; <body><?phpecho "<H4>Are you a Male</h4>"&nbsp;?><form action ="ifstat.php" method ="post">&nbsp; Enter Please: <input type="text" name="val1">&nbsp; <input type ="submit" >&nbsp; <input type ="reset"></form><?php&nbsp; $isMale=$_POST["val1"];if($isMale =="y" || $isMale =="yes"){&nbsp; echo("You are a Male");} elseif($isMale =="n" || $isMale =="no") {&nbsp; echo("You are a female");} else{&nbsp; echo("Invalid entry");}?></body></html>

有只小跳蛙

在你的if陈述中......if($isMale =="y" || "yes" ){the|| "yes"无效,并且总是(在这种情况下)评估为真。您也不会在处理之前检查是否输入了任何内容,因此通常您会检查是否设置了某些内容(即是否已提交表单)然后进行处理...<?phpif (isset($_POST["val1"])) {&nbsp; &nbsp; $isMale=$_POST["val1"];&nbsp; &nbsp; if($isMale =="y" || $isMale =="yes"){&nbsp; &nbsp; &nbsp; &nbsp;echo("You are a Male");&nbsp; &nbsp; } elseif($isMale =="n" || $isMale =="no") {&nbsp; &nbsp; &nbsp; &nbsp;echo("You are a female");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp;echo("Invalid entry");&nbsp; &nbsp; }}?>
随时随地看视频慕课网APP
我要回答