Q:

Radio buttons in PHP not working properly

I have this code:
<?php 
if (isset($_POST['ans'])) {
    $answer = $_POST['ans']; 
    if ($answer == "ans1") {          
        echo 'Correct';      
    }
    else {
        echo 'Incorrect';
    }
}
?>
<form action="example.php" method="post">
  Answer 1 <input type="radio" name="ans" value="ans1" /><br />
  Answer 2 <input type="radio" name="ans" value="ans2"  /><br />
  Answer 3 <input type="radio" name="ans" value="ans3"  /><br />
  Answer 4 <input type="radio" name="ans" value="ans4"  /><br />
  <input type="submit" value="submit" />
</form>

When I select the wrong button, it tells me incorrect and when I select the right one it tells me correct. The problem is that if no button is selected nothing happens. It does not echo incorrect or correct. Even if the answer is nothing then it should be incorrect.

A:

Try to add else if so that if a button selected is empty it wil pass to it.
    <?php
if (isset($_POST['ans'])) {
    $answer = $_POST['ans']; 
    if ($answer == "ans1") {          
        echo 'Correct';      
    }
    else if(empty($answer)){
        echo 'Select Button';
    }
    else {
        echo 'Incorrect';
    }

}
?>

