title: Making form fields nonempty and calculating averages from form data involving some empty fields using php
body: <p>I've been trying to:(i) make my form fields nonempty using php scripts and
  (ii) calculate average of numerical form data inserted into an array and an element of the array could be empty. For instance, suppose the form is used to harvest students' scores, some student might not offer an optional subject. How to adapt the count() array function in this instance is my headache. I admit that some folks have posed  questions that partially addressed it but some issues remain unsolved,  hence this post.
  In summary:(i) the code below executes even when some form fields are empty, against my wish.(ii) If I grant some form fields to be empty; the result of the average calculation is incorrect; also against my wish
  Code:</p>
  
  <pre><code>&lt;?php
  $name = $_POST['candidate'];
  $Eng_CA = $_POST['Eng_CA'];
  $Eng_Ex = $_POST['Eng_Ex'];
  $Math_CA = $_POST['Math_CA'];
  $Math_Ex = $_POST['Math_Ex'];
  $Comp_CA = $_POST['Comp_CA'];
  $Comp_Ex = $_POST['Comp_Ex'];
  $engSum = $Eng_CA + $Eng_Ex;
  $mathSum = $Math_CA + $Math_Ex;
  $compSum = $Comp_CA + $Comp_Ex;
  $tot = array();
  $tot[] = $engSum;
   $tot[]=  $mathSum;
  $tot[] = $compSum;
   $total = array_sum($tot);
   $Average = $total/count($tot); // Average is incorrcet if some fields are empty
  $notEmpty = array();
  
  $notEmpty = array('$name', '$Eng_CA', '$Eng_Ex', '$Math_CA', '$Math_Ex', '$Comp_CA', '$Comp_Ex');
  
  foreach ($notEmpty as $notEmp){ // this is not working; code executes anyway
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;form action ='test_Code.php' method ='post'&gt;
  &lt;table width ='600'&gt;
  &lt;tr&gt;&lt;td&gt; Cand Name&lt;/td&gt;&lt;td colspan ='2'&gt;&lt;input type ='text' name = 'candidate' size = '60'&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;th&gt;Subject&lt;/th&gt;&lt;th&gt;CA&lt;/th&gt;&lt;th&gt;Exam&lt;/th&gt;
  
  &lt;tr&gt;&lt;td&gt;Eng&lt;/td&gt;&lt;td&gt;&lt;input type ='text' name = 'Eng_CA'/&gt;&lt;/td&gt;&lt;td&gt;&lt;input type ='text' name = 'Eng_Ex'/&gt;&lt;/td&gt;&lt;/tr&gt;
  
  &lt;tr&gt;&lt;td&gt;Math&lt;/td&gt;&lt;td&gt;&lt;input type ='text' name = 'Math_CA'/&gt;&lt;/td&gt;&lt;td&gt;&lt;input type ='text' name = 'Math_Ex'/&gt;&lt;/td&gt;&lt;/tr&gt;
  
  &lt;tr&gt;&lt;td&gt;Computer&lt;/td&gt;&lt;td&gt;&lt;input type ='text' name = 'Comp_CA'/&gt;&lt;/td&gt;&lt;td&gt;&lt;input type ='text' name = 'Comp_Ex'/&gt;&lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p></p>
  
  <p></p>
  
  <pre><code>if(empty($notEmp)){
      echo"You have left some fields empty, fill them pls";
  die();
  </code></pre>
  
  <p>}</p>
  
  <pre><code>}
  echo"&lt;table border ='1'&gt;";
  echo"&lt;th&gt;Candidate&lt;/th&gt;&lt;th&gt;Eng Ca&lt;/th&gt;&lt;th&gt;Eng Ex&lt;/th&gt;&lt;th&gt;Math Ca&lt;/th&gt; &lt;th&gt;Math Ex&lt;/th&gt; &lt;th&gt;Comp Ca&lt;/th&gt;&lt;th&gt;Comp ex&lt;/th&gt;&lt;th&gt;Total&lt;/th&gt;&lt;th&gt;Average&lt;/th&gt;";
  echo"&lt;tr&gt;&lt;td&gt;";
  echo $name ."&lt;/td&gt;&lt;td&gt;".$Eng_CA."&lt;/td&gt;&lt;td&gt;".$Eng_Ex."&lt;/td&gt;&lt;td&gt;".$Math_CA."&lt;/td&gt;&lt;td&gt;".$Math_Ex."&lt;/td&gt;&lt;td&gt;".$Comp_CA."&lt;/td&gt;&lt;td&gt;". $Comp_Ex."&lt;/td&gt;&lt;td&gt;".$total."&lt;/td&gt;&lt;td&gt;".$Average."&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;";
  
  ?&gt;
  </code></pre>