Rookie PHP question, receiving an undefined variable error. Please Help
<p>I am reading a book and going through the examples, unfortunately i keep getting an error if i leave a field empty when i submit the form. I checked the errata and there was nothing, i tried posting on the books forums but I didn't get any responses. </p>

<p>I believe that maybe i have to declare the variables first but that would take away from the variables being automatically generated.</p>

<p>Any help would be greatly appreciated, i am a rookie trying to learn php.</p>

<p>per request, here is the $required and $expected. Thanks!</p>

<pre><code>$expected = array('name', 'email', 'comments');
$required = array('name', 'email', 'comments');

&lt;?php
foreach ($_POST as $key =&gt; $value){
    //assign to temporary variable and strip whitespace if not an array
    $temp = is_array($value) ? $value : trim($value);

    //if empty and required, add to $missing array
    if (empty($temp) &amp;&amp; in_array($key, $required)){
        $missing[] = $key;
    } elseif(in_array($key, $expected)){
        //otherwise, assign to a variable of the same name as $key
        ${$key} = $temp;
    }
}

&lt;?php 
include('./includes/title.inc.php'); 
$errors = array();
$missing = array();

//Check to see if the form has been submitted
if (isset($_POST['send'])){
    //email processing script
    $to = 'drewdin1@hotmail.com'; //use your email address
    $subject = 'Feedback from Japan Journey';

    //list expecting fields
    $expected = array('name', 'email', 'comments');

    //set required fields
    $required = array('name', 'email', 'comments');
    include('./includes/processmail.inc.php'); 
}
?&gt;
&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=utf-8"&gt;
&lt;title&gt;Japan Journey&lt;?php if (isset($title)){echo "&amp;#8212;{$title}";} ?&gt;&lt;/title&gt;
&lt;link href="styles/journey.css" rel="stylesheet" type="text/css" media="screen"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="header"&gt;
    &lt;h1&gt;Japan Journey&lt;/h1&gt;
&lt;/div&gt;
&lt;div id="wrapper"&gt;
    &lt;?php include('./includes/menu.inc.php'); ?&gt;
    &lt;div id="maincontent"&gt;
        &lt;h2&gt;Contact Us&lt;/h2&gt;
        &lt;?php if ($missing || $errors){ ?&gt;
            &lt;p class="warning"&gt;Please fix the item(s) indicated.&lt;/p&gt;
        &lt;?php } ?&gt;
      &lt;p&gt;Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.&lt;/p&gt;
        &lt;form id="feedback" method="POST" action=""&gt;
            &lt;p&gt;
                &lt;label for="name"&gt;Name:
                    &lt;?php if ($missing &amp;&amp; in_array('name', $missing)){ ?&gt;
                        &lt;span class="warning"&gt;Please enter your name&lt;/span&gt;
                    &lt;?php } ?&gt;
                &lt;/label&gt;
                &lt;input name="name" id="name" type="text" class="formbox"&lt;?php
                    if ($missing || $errors){
                        echo ' value="' . htmlentities($name, ENT_COMPAT, 'UTF-8') . '" ';
                    } ?&gt; /&gt;
            &lt;/p&gt;
            &lt;p&gt;
                &lt;label for="email"&gt;Email:
                    &lt;?php if ($missing &amp;&amp; in_array('email', $missing)){ ?&gt;
                        &lt;span class="warning"&gt;Please enter your email address&lt;/span&gt;
                    &lt;?php } ?&gt;
                &lt;/label&gt;
                &lt;input name="email" id="email" type="text" class="formbox"&lt;?php
                    if ($missing || $errors){
                        echo ' value="' . htmlentities($email, ENT_COMPAT, 'UTF-8') . '" ';
                    } ?&gt; /&gt;
            &lt;/p&gt;
            &lt;p&gt;
                &lt;label for="comments"&gt;Comments:
                    &lt;?php if ($missing &amp;&amp; in_array('comments', $missing)){ ?&gt;
                        &lt;span class="warning"&gt;Please enter your comments&lt;/span&gt;
                    &lt;?php } ?&gt;
                &lt;/label&gt;
                &lt;textarea name="comments" id="comments" cols="60" rows="8"&gt;&lt;?php
                    if ($missing || $errors){
                        echo htmlentities($comments, ENT_COMPAT, 'UTF-8');
                    } ?&gt;&lt;/textarea&gt;
            &lt;/p&gt;
            &lt;p&gt;
                &lt;input name="send" id="send" type="submit" value="Send message"&gt;
            &lt;/p&gt;
        &lt;/form&gt;

        &lt;pre&gt;
            &lt;?php if ($_POST &amp;&amp; $missing) {print_r($_POST);} ?&gt;
        &lt;/pre&gt;
    &lt;/div&gt;
    &lt;?php include('./includes/footer.inc.php'); ?&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p><img src="http://i.stack.imgur.com/GKXAH.jpg" alt="enter image description here"></p>
