title: Undefined variable parameter in function
body: <p>An assignment question reads as follow</p>
  <p><a href="https://i.stack.imgur.com/WI0k7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WI0k7.jpg" alt="enter image description here" /></a></p>
  <p>I have created the function, as follows:</p>
  <pre><code>function getToppings($toppings){
      if($toppings=='') {
          echo 'You have to select at least one topping';
         die();
      }//if
          foreach($toppings as $index =&gt; $selected) {
  
              $toppings[$index] = $selected;
              echo $selected;
              echo '&lt;br /&gt;';
          }//foreach
  }//function
  </code></pre>
  <p><strong>HTML</strong></p>
  <h3>Select Your Topping</h3>
  <pre><code>&lt;form name=&quot;select-topping&quot; method=&quot;post&quot;&gt;
      &lt;label for=&quot;toppings&quot;&gt;Peperoni&lt;/label&gt;
      &lt;input type=&quot;checkbox&quot; name=&quot;toppings[]&quot; value=&quot;peperoni&quot;&gt;
      &lt;br /&gt;
      &lt;label for=&quot;toppings&quot;&gt;Salami&lt;/label&gt;
      &lt;input type=&quot;checkbox&quot; name=&quot;toppings[]&quot; value=&quot;salami&quot;&gt;
      &lt;br /&gt;
      &lt;label for=&quot;toppings&quot;&gt;Ham&lt;/label&gt;
      &lt;input type=&quot;checkbox&quot; name=&quot;toppings[]&quot; value=&quot;ham&quot;&gt;
      &lt;br &gt;
      &lt;label for=&quot;toppings&quot;&gt;Mushroom&lt;/label&gt;
      &lt;input type=&quot;checkbox&quot; name=&quot;toppings[]&quot; value=&quot;mushroom&quot;&gt;
      &lt;button type=&quot;submit&quot; name=&quot;submit&quot; class=&quot;btn btn danger&quot;&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  <p><strong>Function Call</strong></p>
  <pre><code>if(isset($_POST['submit'])){
      getToppings($_POST['toppings']);
  }
  </code></pre>
  <p>The functions works perfectly, and prints all toppings, selected, however my problem comes in when no toppings are selected (validation)</p>
  <h2>MY PROBLEM</h2>
  <p>When no topping is selected I get the following, error:</p>
  <p><a href="https://i.stack.imgur.com/TqTEs.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TqTEs.jpg" alt="enter image description here" /></a></p>
  <p>So eventhough my form validates, I still get the error undefined variable:</p>
  <p><em>Now I am pretty sure I know why I get the error message, it is because if no toppings were selected, no parameters gets passed to function...correct?</em> What I do not know is how to fix / improve on it...any advice or help appreciated.</p>