title: PHP: a short cut for isset and !empty?
body: <p>I wonder if there any better ideas to solve the problem below,</p>
  <p>I have a form with a number of input fields, such as,</p>
  <pre><code>&lt;input name=&quot;pg_title&quot; type=&quot;text&quot; value=&quot;&quot; /&gt;
  &lt;input name=&quot;pg_subtitle&quot; type=&quot;text&quot; value=&quot;&quot; /&gt;
  &lt;input name=&quot;pg_description&quot; type=&quot;text&quot; value=&quot;&quot; /&gt;
  &lt;input name=&quot;pg_backdate&quot; type=&quot;text&quot; value=&quot;&quot; /&gt;
  etc
  </code></pre>
  <p>But sometimes don't need certain input fields above in my form, for instance, I only need the page title for my db injection,</p>
  <pre><code>&lt;input name=&quot;pg_title&quot; type=&quot;text&quot; value=&quot;&quot; /&gt;
   etc
  </code></pre>
  <p>And I have another php page to handle the <code>$_POST</code> data,</p>
  <pre><code>$pg_title = null;
  $pg_subtitle = null;
  $pg_description = null;
  $pg_backdate = null;
  
   if(isset($_POST['pg_title']) &amp;&amp; !empty($_POST['pg_title']) ) $pg_title = $_POST['pg_title'];
   if(isset($_POST['pg_subtitle']) &amp;&amp; !empty($_POST['pg_subtitle']) ) $pg_subtitle = $_POST['pg_subtitle'];
   if(isset($_POST['pg_description']) &amp;&amp; !empty($_POST['pg_description']) ) $pg_description = $_POST['pg_description'];
   if(isset($_POST['pg_backdate']) &amp;&amp; !empty($_POST['pg_backdate']) ) $pg_backdate = $_POST['pg_backdate'];
  </code></pre>
  <p>Every time I will have to check if the <code>$_POST</code> of a certain input field is set and <code>not empty</code>, otherwise its variable will be set to <code>null</code>, so that I won't inject an empty space into my DB.</p>
  <p>I find the <code>isset</code> and <code>!empty</code> in the if-condition are very repetitive when I have a long list of variables to handle.</p>
  <p>Is there any default PHP function to '<strong>shorten</strong>' the process above? Or do I have to write a user-defined function to handle this?</p>
  <p>Or maybe there is another way to do this?</p>
  <p>Just some extra code in my php page that handle the $_POST data,</p>
  <pre><code>$sql = &quot;
      UPDATE root_pages
      SET 
          pg_url = ?, 
          pg_title = ?,
          pg_subtitle = ?,
          pg_backdate = ?,
          pg_description = ?,     
          ...
          updated_by = ?
      WHERE pg_id = ?
      &quot;;
          
      $result = $connection-&gt;run_query($sql,array(
          $pg_url, 
          $pg_title,
          $pg_subtitle,
          $pg_backdate,
          $pg_description,        
          ...
          $pg_id
          ));
  </code></pre>
  <p>as you see that <code>$pg_subtitle, $pg_backdate, $pg_description, etc</code> always present in my query. so if I get <code>$pg_subtitle = ''</code> instead of <code>$pg_subtitle = null</code> when there is no data in it, my db record will have an empty space for that column.</p>