[英]Need help regarding this if else program in PHP
<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>
So, this is my program code.所以,这是我的程序代码。 I always keep on getting You are a Male as the output even though I type in "n" or some other value rather than y.
尽管我输入了“n”或其他值而不是 y,但我总是继续将 You are a Male 作为输出。 What's wrong with the code here?
这里的代码有什么问题?
Here is the photo of the output https://imgur.com/a/PPH49GK这是输出的照片https://imgur.com/a/PPH49GK
You need to change your condition formatting from if($isMale =="y" || "yes"){
to if($isMale =="y" || $isMale =="yes"){
您需要将条件格式从
if($isMale =="y" || "yes"){
更改为if($isMale =="y" || $isMale =="yes"){
<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" || $isMale =="yes"){
echo("You are a Male");
} elseif($isMale =="n" || $isMale =="no") {
echo("You are a female");
} else{
echo("Invalid entry");
}
?>
</body>
</html>
In your if
statements....在你的
if
语句中......
if($isMale =="y" || "yes" ){
the || "yes"
|| "yes"
|| "yes"
isn't valid and will always (in this case) evaluate to true. || "yes"
是无效的,并且总是(在这种情况下)评估为真。
You also don't check if anything has been entered before processing this, so normally you would do a check of if something is set (ie has the forma been submitted) and then do the processing...在处理此之前,您也不检查是否输入了任何内容,因此通常您会检查是否设置了某些内容(即是否已提交表单),然后进行处理...
<?php
if (isset($_POST["val1"])) {
$isMale=$_POST["val1"];
if($isMale =="y" || $isMale =="yes"){
echo("You are a Male");
} elseif($isMale =="n" || $isMale =="no") {
echo("You are a female");
} else {
echo("Invalid entry");
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.