title: How to avoid undefined variable in php when variable does not exist?
body: <p>I only touch php on the very basic stuff.</p>
  
  <p>But I always like to avoid errors/notices when making wordpress themes.</p>
  
  <p>I have a simple function below that I made to list my taxonomy terms.</p>
  
  <pre><code>$tax = 'dealer-communications';
  $terms = get_terms($tax);
  $count = count($terms);
  if ( $count &gt; 0 ){
      echo '&lt;li class="nav-header"&gt;Dealer communications&lt;/li&gt;';
      foreach ( $terms as $term ) {
          if (get_queried_object()-&gt;slug == $term-&gt;slug) $active = 'class="active"';
          echo '&lt;li '.$active.'&gt;&lt;a href="'.get_term_link($term-&gt;slug, $tax).'"&gt;' . $term-&gt;name . '&lt;/a&gt;&lt;/li&gt;';
      }
  }
  </code></pre>
  
  <p><br /></p>
  
  <p>As you can see I have a <code>$active</code> variable.</p>
  
  <p>This <code>active</code> variable is undefined when the my <code>get_queried_object()-&gt;slug</code> does not match <code>$term-&gt;slug</code></p>
  
  <p>How can I avoid my active variable from being undefined. So it is defined but empty.</p>
  
  <p>They only way my brain could work it out is by doing this...</p>
  
  <pre><code>$active = null;
  if (get_queried_object()-&gt;slug == $term-&gt;slug) $active = 'class="active"';
  </code></pre>
  
  <p>or...</p>
  
  <pre><code>if (get_queried_object()-&gt;slug == $term-&gt;slug) {
      $active = 'class="active"';
  } else {
      $active = '';
  }
  </code></pre>
  
  <p><br /></p>
  
  <p>Is this the most efficient way of doing this or is there an alternative php method?</p>
  
  <p><br /></p>
  
  <p>Many Thanks
  Josh</p>