Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
+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 '11 at 12:04
    
Does this actually have anything to do with WordPress? Seems like a general PHP question to me. –  anu May 13 '11 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 '11 at 12:45
    
@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 '11 at 13:04
    
@anu it is mostly PHP, but transients and how to handle get_transient() return value is highly WP-specific. This is quite valid question as for me. @VicePrez You are putting way to much meaning on downvote. It's normal and common that someone didn't like a question, by no means it is insult or anything. :) –  Rarst May 13 '11 at 13:05
show 2 more comments

3 Answers

up vote 3 down vote accepted

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;
share|improve this answer
    
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 '11 at 11:31
    
I've rolled back my edit as the first time round the first example will work fine. –  Brady May 13 '11 at 11:52
add comment

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.

share|improve this answer
    
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 '11 at 11:49
    
@viceprez yeah it would be fine to reverse –  Brady May 13 '11 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 '11 at 11:55
    
alright. might as well stick to the standard. Thanks for helping out. –  VicePrez May 13 '11 at 12:02
add comment

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.

share|improve this answer
    
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 –  Brady May 13 '11 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 '11 at 11:34
    
Brady I would have comment but I didn't had privilege until now. :) –  Hameedullah Khan May 13 '11 at 11:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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