phpconditional-statements

Check if variable contains one of multiple qualifying values


<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. What's wrong with the code here?

Here is the photo of the output https://i.sstatic.net/Fpk5U.jpg


Solution

  • You need to change your condition formatting from if($isMale =="y" || "yes"){ to 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>