当变量不存在时,如何在php中避免未定义的变量?

I only touch php on the very basic stuff.

But I always like to avoid errors/notices when making wordpress themes.

I have a simple function below that I made to list my taxonomy terms.

$tax = 'dealer-communications';
$terms = get_terms($tax);
$count = count($terms);
if ( $count > 0 ){
    echo '<li class="nav-header">Dealer communications</li>';
    foreach ( $terms as $term ) {
        if (get_queried_object()->slug == $term->slug) $active = 'class="active"';
        echo '<li '.$active.'><a href="'.get_term_link($term->slug, $tax).'">' . $term->name . '</a></li>';
    }
}


As you can see I have a $active variable.

This active variable is undefined when the my get_queried_object()->slug does not match $term->slug

How can I avoid my active variable from being undefined. So it is defined but empty.

They only way my brain could work it out is by doing this...

$active = null;
if (get_queried_object()->slug == $term->slug) $active = 'class="active"';

or...

if (get_queried_object()->slug == $term->slug) {
    $active = 'class="active"';
} else {
    $active = '';
}


Is this the most efficient way of doing this or is there an alternative php method?


Many Thanks Josh

There is no alternative php method, but for readability you should not declare/init your variables in a if block, e.g. :

foreach ( $terms as $term ) {
    $active = '';
    if (get_queried_object()->slug == $term->slug) $active = 'class="active"';
    echo '<li '.$active.'><a href="'.get_term_link($term->slug, $tax).'">' . $term->name . '</a></li>';
}

You can also use ternary operator (but not really readable) :

$active = (get_queried_object()->slug == $term->slug) ? 'class="active"' : '';

The 2nd one will be the more efficient way :

if (get_queried_object()->slug == $term->slug) {
    $active = 'class="active"';
} else {
    $active = '';
}

The second option is the best approach as it makes sure the variable value actually changes on every loop, otherwise there is a possibility that the value of the previous loop will affect the current one.

For example, you get to an active LI, set $active to the correct value and all works ok. However, on next loop step the LI should not be active, but because you are not clearing the previous assignment the $active variable will still set this LI to active.

EDIT: PHP scope does not work like javascript scope, some coments on this answer seem to require this clarification:

$test = array(
  array("name"=>"Is inactive", "active"=>false),
  array("name"=>"Is active", "active"=>true),
  array("name"=>"Is caught by problem", "active"=>false)
);

foreach ($test as $example ){
  if ($example["active"]) $active = true;

  if ($active) {
    echo $example["name"]." was parsed as ACTIVE
";
  } else {
    echo $example["name"]." was parsed as INACTIVE
";
  }
}

Outputs a notice (because $active is undefined on the first step of the loop) and the following text:

Is inactive was parsed as INACTIVE
Is active was parsed as ACTIVE
Is caught by problem was parsed as ACTIVE <--- Problem

The second approach is more common to see in my experience. You could of course shorten this using a ternary operator, similar to:

$active = (get_queried_object()->slug == $term->slug) ? 'class="active"' : '';
//       if ^                                            ^ do this        ^ else do this    

Some consider this to be more confusing though. I guess that boils down to personal preference.

You could also use the ternary operator as it's a bit shorter:

$active = ( get_queried_object()->slug == $term->slug ? 'class="active"' : '' );