PHP empty() error message
<p>I have just started learning and have put together the form below. The confusion I have is when I use empty to validate if the user selected or entered any information, the code generates the error below. </p>

<p>I noticed if I have the following lines of code</p>

<pre><code>if(empty($gender)) {
        $errormessage[2] = "Please select your gender";
    } 
</code></pre>

<p>as</p>

<pre><code>if(empty($_POST["gender"])) {
        $errormessage[2] = "Please select your gender";
    }
</code></pre>

<p>and </p>

<pre><code>if(empty($gender)) {
        $errormessage[2] = "Please select your gender";
    }
</code></pre>

<p>as</p>

<pre><code>if(empty($_POST["gender"])) {
        $errormessage[2] = "Please select your gender";
    }
</code></pre>

<p>I do not see the error message. I take it that error message is being generated because of the lines of code $_POST for the gender and media form elements which are radio buttons and checkboxes respectively. However if I want to initialize all the variables at the top, what is the best way to do so?</p>

<pre><code>/* &lt;?php

if(isset($_POST["submit"])) {

    $fname = $_POST["fname"];
    $lname = $_POST["lname"];
    $gender = $_POST["gender"];
    $age = $_POST["age"];
    $address = $_POST["address"];
    $media = $_POST['media'];

    $errormessage =  array();

    if(empty($fname)) {
        $errormessage[0] = "Please enter your first name";
    }

    if(empty($lname)) {
        $errormessage[1] = "Please enter your last name";
    }

    if(empty($gender)) {
        $errormessage[2] = "Please select your gender";
    }

    if(empty($age)) {
        $errormessage[3] = "Please select your age";
    }

    if(empty($address)) {
        $errormessage[4] = "Please enter your address";
    }

    if(empty($media)) {
        $errormessage = "Please select the type of media";

    }

}
?&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Sample Registration&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h2&gt;Sample registration&lt;/h4&gt;
        &lt;form name="registration" method="post" action="registration.php"&gt;
            &lt;div&gt;
                First Name: &lt;br /&gt;
                &lt;input type="text" name="fname" value=""&gt;
            &lt;/div&gt;

            &lt;div&gt;
                Last Name: &lt;br /&gt;
                &lt;input type="text" name="lname" value=""&gt;
            &lt;/div&gt;

            &lt;div&gt;
                Gender: &lt;br /&gt;
                male&lt;input type="radio" name="gender" value="male"&gt;
                female&lt;input type="radio" name="gender" value="female"&gt;
            &lt;/div&gt;

            &lt;div&gt;
                Age: &lt;br /&gt;
                &lt;select name="age"&gt;
                    &lt;option value=""&gt;Please select your age&lt;/option&gt;
                    &lt;option value="18-25"&gt;18-25&lt;/option&gt;
                    &lt;option value="26-33"&gt;26-33&lt;/option&gt;
                &lt;/select&gt;
            &lt;/div&gt;

            &lt;div&gt;
                Address: &lt;br /&gt;
                &lt;textarea name="address" cols="10" rows="10"&gt;&lt;/textarea&gt;
            &lt;/div&gt;

            &lt;div&gt;
                Sign-me up: &lt;br /&gt;
                &lt;input type="checkbox" name="media['newsletter']" value="newsletter"&gt; newsletter
                &lt;input type="checkbox" name="media['specials']" value="specials"&gt; specials
                &lt;input type="checkbox" name="media['events']" value="events"&gt; events

            &lt;div&gt;
                &lt;input type="submit" name="submit" value="submit"&gt;
            &lt;/div&gt;
        &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

*/
</code></pre>

<p>/* Error Message */</p>

<pre><code>! ) Notice: Undefined index: gender in C:\Program Files\EasyPHP-5.3.5.0\www\registration.php on line 7
Call Stack
#   Time    Memory  Function    Location
1   0.0004  341792  {main}( )   ..\registration.php:0
Dump $_SERVER

$_SERVER['REMOTE_ADDR'] =



string '127.0.0.1' (length=9)

$_SERVER['REQUEST_METHOD'] =



string 'POST' (length=4)

$_SERVER['REQUEST_URI'] =



string '/registration.php' (length=17)

Variables in local scope (#1)

$address =

    Undefined

$age =

    Undefined

$errormessage =

    Undefined

$errormsg =

    Undefined

$fname =



string '' (length=0)

$gender =

    Undefined

$lname =



string '' (length=0)

$media =

    Undefined

( ! ) Notice: Undefined index: media in C:\Program Files\EasyPHP-5.3.5.0\www\registration.php on line 10
Call Stack
#   Time    Memory  Function    Location
1   0.0004  341792  {main}( )   ..\registration.php:0
Variables in local scope (#1)

$address =



string '' (length=0)

$age =



string '' (length=0)

$errormessage =

    Undefined

$errormsg =

    Undefined

$fname =



string '' (length=0)

$gender =



null

$lname =



string '' (length=0)

$media =

    Undefined
</code></pre>
