3
\$\begingroup\$

Is this the best way to prevent that foreach loop from happening if $terms doesn't exist?

$terms = get_the_terms( $postid, 'fecha' );
if(!empty($terms)) {
    foreach($terms as $term) {
        $myterm = $term->slug;
        if(in_array($myterm, $queried_terms)) {continue;}
        $queried_terms[] = $myterm;
    }
}
\$\endgroup\$
5
\$\begingroup\$

The loop won't be executed if $terms is false/null/an empty array.

As such: you don't need the empty check. It doesn't hurt, but it's simply not required.

If $terms is sometimes not an array

You can just cast it to an array before use:

$terms = get_the_terms( $postid, 'fecha' );
foreach((array) $terms as $term) {
    $myterm = $term->slug;
    if(in_array($myterm, $queried_terms)) {
        continue;
    }
    $queried_terms[] = $myterm;
}

The code would work with or without casting to an array - the only difference is that if $terms is not an array, a notice would be thrown without it.

if this is part of another function

you can simply return-early to keep your code concise:

function foo() {
    $terms = get_the_terms( $postid, 'fecha' );
    if (!$terms) {
        return array();
    }

    $queried_terms = array();
    foreach($terms as $term) {
        $myterm = $term->slug;
        if(in_array($myterm, $queried_terms)) {
            continue;
        }
        $queried_terms[] = $myterm;
    }
    return $queried_terms;
}

But, any way you slice it - the empty check isn't strictly required, and deeper code nesting is something to be aware of and avoid.

\$\endgroup\$
  • \$\begingroup\$ +1 However I question the wisdom in typecasting a variable like that. A variable, in my opinion, should not change types so frequently that you would not know its type. If you find yourself doing this then you should probably implement new variables into your program for those other types. At most my variables are either a boolean or of the desired type so that I can check if it did indeed pass some check or has the value I am looking for. Just my two cents :) \$\endgroup\$ – mseancole May 16 '12 at 13:12
  • \$\begingroup\$ if terms is either an array or falsey - it's fine and appropriate. \$\endgroup\$ – AD7six May 16 '12 at 13:20
  • \$\begingroup\$ It still should be used with caution. That's all I was trying to point out. I personally wouldn't use it, but I know many programmers do. That's why I was trying to explain that it shouldn't be used to force your code to work. \$\endgroup\$ – mseancole May 16 '12 at 13:25
  • \$\begingroup\$ I agree with your sentiment, but get_the_terms sounds like wordpress. That probably gives some indication of expectations and the environment (notices ignored, because otherwise you'd drown in them). Casting to an array only avoids a notice and only "If $terms is sometimes not an array" - IME you can't trust anything coming out of wordpress to consistently be what you expect. Casting variables in and of itself is not a bad thing - especially if you work in a type-sensitive system (e.g. mongo as db, json as output). \$\endgroup\$ – AD7six May 16 '12 at 13:32
1
\$\begingroup\$
$terms = get_the_terms( $postid, 'fecha' );

if(is_array($terms)) {
   foreach($terms as $term) {
       $myterm = $term->slug;
       if(in_array($myterm, $queried_terms)) {continue;}
       $queried_terms[] = $myterm;
   }
}

I always use this method and never get problem

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.