title: How to loop through a list of variables and check if their values is not empty in php
body: <p>Good Day, I'm trying to check if the value of some variables which is passed from ajax is not empty. I have validated this in jQuery (ajax) but trying to add just an extra layer in php to check if the $_POST values are not empty. Below are the variables;</p>
  
  <pre><code>$_smChest           = wp_strip_all_tags( $_POST['send_smChest'] );
  $_smSleeve          = wp_strip_all_tags( $_POST['send_smSleeve'] );
  $_smShoulder        = wp_strip_all_tags( $_POST['send_smShoulder'] );
  $_smJacketLength    = wp_strip_all_tags( $_POST['send_smJacketLength'] );
  $_smArmHole         = wp_strip_all_tags( $_POST['send_smArmHole'] );
  $_smBicep           = wp_strip_all_tags( $_POST['send_smBicep'] );
  $_smStomach         = wp_strip_all_tags( $_POST['send_smStomach'] );
  $_smFrontWidth      = wp_strip_all_tags( $_POST['send_smFrontWidth'] );
  $_smBackWidth       = wp_strip_all_tags( $_POST['send_smBackWidth'] );
  $_smWaistBand       = wp_strip_all_tags( $_POST['send_smWaistBand'] );
  $_smPantsLength     = wp_strip_all_tags( $_POST['send_smPantsLength'] );
  $_smSeat            = wp_strip_all_tags( $_POST['send_smSeat'] );
  $_smStride          = wp_strip_all_tags( $_POST['send_smStride'] );
  $_smthighs          = wp_strip_all_tags( $_POST['send_smthighs'] );
  $_smKnee            = wp_strip_all_tags( $_POST['send_smKnee'] );
  $_smLegOpening      = wp_strip_all_tags( $_POST['send_smLegOpening'] );
  $_smJacketFit       = wp_strip_all_tags( $_POST['send_smJacketFit'] );
  $_smTrouserFit      = wp_strip_all_tags( $_POST['send_smTrouserFit'] );
  </code></pre>
  
  <p>I saw this question <a href="https://stackoverflow.com/questions/18578968/php-check-if-all-variables-are-empty">(PHP Check if all variables are empty)</a> and used this code to check if values of the variables are not empty;</p>
  
  <pre><code>$user = wp_get_current_user();
  if (!empty($_POST)) {
      foreach ($_POST as $value) {
          if (!empty($value)) {
            //Then Updated the users profile with the value and echo 1 for Ajax Response
             update_user_meta( $user-&gt;ID, 'sm_chest', $_smChest);
             update_user_meta( $user-&gt;ID, 'sm_sleeve', $_smSleeve);
             update_user_meta( $user-&gt;ID, 'sm_shoulder', $_smShoulder);
             update_user_meta( $user-&gt;ID, 'sm_jacket_length', $_smJacketLength);
             //(...)
             echo '1';
          }else {
            // echo '0' for Ajax response
            echo '0';
          }
      }
  }
  </code></pre>
  
  <p>When i did it this way, the user profile got updated which is fine but the ajax response was <code>11111111111111111110</code> instead of <code>1</code> [see image below]</p>
  
  <p><a href="https://i.stack.imgur.com/trAcK.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/trAcK.jpg" alt="enter image description here"></a> </p>
  
  <p>I know i can use something like this [below code] but the if statement would be really long. That's why i'm looking to use a more optimized approach</p>
  
  <pre><code>if (!empty($_smChest) &amp;&amp; !empty($_smSleeve) &amp;&amp; !empty($_smShoulder) &amp;&amp; !empty($_smJacketLength) &amp;&amp; //(...) ) {
      //Then Updated the users profile with the value and echo 1 for Ajax Response
      update_user_meta( $user-&gt;ID, 'sm_chest', $_smChest);
      update_user_meta( $user-&gt;ID, 'sm_sleeve', $_smSleeve);
      update_user_meta( $user-&gt;ID, 'sm_shoulder', $_smShoulder);
      update_user_meta( $user-&gt;ID, 'sm_jacket_length', $_smJacketLength);
      //(...)
         echo '1';
  }else {
      echo '0';
  }
  </code></pre>
  
  <p>Not sure what i'm doing wrong or why i'm getting lots of <code>1s</code> and a <code>0</code>. </p>
  
  <p>What i would like to achieve is to;<br>
  1. Loop through all the values of each variable and make sure none is empty and;<br>
  2. Make the response return <code>1</code> or <code>0</code>, if values are <b>not empty</b> or <b>empty</b> respectively</p>
  
  <p>An idea on what i can do differently to make this work would be highly appreciated.</p>