2

Im using a WordPress transient, which expires after an hour, to store a value which is an integer. Im trying to use switch() and multiple case() statements to evaluate if the transient exists or not (i.e true or false).

Here are my questions:

  1. Which comparison operator (=, ==, ===), in example 2, is ideal for this context?
  2. Which of the following examples is appropriate?
  3. Would the following examples yield the same result?

Example 1:

$transient = get_transient( 'foobar' );

switch( $transient ) :

    case( true ) :
        // do stuff
    break;

    case( false ) :
        // do stuff
    break;

endswitch;

versus

Example 2:

$transient = get_transient( foobar );

switch( $transient ) :

    case( $transient = true ) :
        // do stuff
    break;

    case( $transient = false ) :
        // do stuff
    break;

endswitch;

Thanks in advance.

7
  • +1 to @brady @Rarst and @Hameedullah for helping out. I'll just stick to if/else statements for this example. Thanks guys.
    – VicePrez
    May 13, 2011 at 12:04
  • Does this actually have anything to do with WordPress? Seems like a general PHP question to me.
    – anu
    May 13, 2011 at 12:16
  • @anu Thanks for the heads up. my bad, want a gold medal? There's no point in voting down a question on the basis of it being exclusive to PHP (WordPress is built on php, if im not wrong?). If thats the case half the questions on WordPress stackexchange should be voted down. Please be constructive and find something else to do other than vote questions down. The question was answered in a professional manner. Case closed.
    – VicePrez
    May 13, 2011 at 12:45
  • 1
    @viceprez - there's no need for that kind of hostility here. WPSE is clearly not here for dealing with simple PHP questions, regardless of the fact that WP is built on PHP. I'm sorry you're offended, but questions that are simply PHP related should be asked over at StackOverflow.
    – anu
    May 13, 2011 at 13:04
  • 1
    @rarst - I hear what you're saying, but to me this question is essentially "how do you deal with true and false values in a switch".
    – anu
    May 13, 2011 at 13:06

3 Answers 3

3

Does it have to use a switch?

why not something like this:

if ( false === ( $value = get_transient( 'value' ) ) ) {
     // this code runs when there is no valid transient set
} else {
    // this code runs when there is a valid transient set
}

If the a transient returns a value it will not return true. It returns the value of the transient. If the transient is not set then it returns bool(false)

if you are using a switch it would be like this:

$transient = get_transient( foobar );

switch( $transient ) :

    case false:
        // transient not set
    break;

    default:
        // transient didnt return false
    break;

endswitch;
2
  • well i don't have to necessarily stick to a switch, i might just use your first example, I'm just experimenting. For the sake of learning something new, lets assume the transient exists with a value of 0, will it be considered true or false? Thanks Brady.
    – VicePrez
    May 13, 2011 at 11:31
  • I've rolled back my edit as the first time round the first example will work fine.
    – Scott
    May 13, 2011 at 11:52
3

I think switch is a little to bulky for this. Also your second example is definitely not a way to do it.

$transient = get_transient( 'foobar' );

if( false !== $transient ) { // boolean false or has no value 

    //code goes here
}
else { // any other case

   //code goes here
}

=== / !== compare by value and type so even if your transient will be integer 0 it won't be considered false.

4
  • yea ive seen this example in one of your posts a while back which is related to extracting the twitter count (or feedburner?) from an xml. Its pretty robust. For the sake of readability could the if statement be reversed (i.e. $transient !== false) ?
    – VicePrez
    May 13, 2011 at 11:49
  • @viceprez yeah it would be fine to reverse
    – Scott
    May 13, 2011 at 11:53
  • 2
    @VicePrez the value going first in comparison is good practice because it will error if you mistakenly use = instead of == (which when unintended will be bug if you had variable first). It might seems weird at first but you get used to it fast and it is included in WP Coding Standards.
    – Rarst
    May 13, 2011 at 11:55
  • alright. might as well stick to the standard. Thanks for helping out.
    – VicePrez
    May 13, 2011 at 12:02
0

Your both examples are wrong for the value returned by get_transient. get_transient returns false when the transient is not found, but what if the transient value is 0, it will also evaluate to false.

The PHP switch statement always does the loose comparison, so you might not get the desired results in case when transient value is 0. So its better to not use switch in this case, instead use if.

Though the Example 1 will work if you are sure your transient values will never be something that will evaluate to false.

3
  • This should be a comment not an answer. Yes you are right it will return false on 0. if the user might have an INT of 0 then the function is_int() can be used instead. I'll update my answer to include this
    – Scott
    May 13, 2011 at 11:22
  • thanks for the heads up. that makes alot of sense. Ill probably go for an if/else structure. I can't guarantee that the values will never equate to zero.
    – VicePrez
    May 13, 2011 at 11:34
  • Brady I would have comment but I didn't had privilege until now. :) May 13, 2011 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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