Mark on WordPress

WordPress puts food on my table.

Doublebang

with 22 comments

Ever seen or written this PHP code?

function foo( $foo ) {
    // magic
    if ( $foo ) // $foo is not bool
        return true;
    else
        return false;
}

So you want to return a boolean value (true/false) but $foo isn’t a bool so you can’t return it directly.

Option 1, cast to boolean:

function foo( $foo ) {
    // magic
    return (boolean) $foo;
}

Option 2, cast to “bool” (shortcut):

function foo( $foo ) {
    // magic
    return (bool) $foo;
}

Option3, doublebang:

function foo( $foo ) {
    // magic
   return !!$foo;
}

The first ! (the “not” operator) casts $foo to a negative bool, and the second one flips it back to the bool representation of its original value.

And of course if you want to return true when the variable evaluates as false, you can just do:

function foo( $foo ) {
    // magic
    return !$foo;
}

Appologies to anyone who has read this far and was expecting pornography.

Written by Mark Jaquith

October 11, 2007 at 1:31 pm

Posted in wordpress

22 Responses

Subscribe to comments with RSS.

  1. Besic thing, love to see.
    Btw, if some porn for extra will be the best :D

    n-blue

    October 11, 2007 at 1:57 pm

  2. Heh. Thanks for this, but I really didn’t know “doublebang” refers to porn. :P

    Aja

    October 11, 2007 at 2:03 pm

  3. (* Stares at Aja and blinks in disbelief *)

    You’re a young pup, still… ;-)

    Mark Steel

    October 11, 2007 at 2:17 pm

  4. Hahahahaha!

    Mark Ghosh

    October 11, 2007 at 2:44 pm

  5. LOL, nice post end xD

    Javier Aroche

    October 11, 2007 at 2:58 pm

  6. Yes, I came here looking for php code…. yes It’s true! XD

    expresion-x

    October 11, 2007 at 3:53 pm

  7. I would have to say that if we are trying to write obtuse but valid php code then either return $foo && $foo or the more elegant, and a favourite of mine return $foo ? true : false

    Peter Westwood

    October 11, 2007 at 4:36 pm

  8. nice job

    taby

    October 11, 2007 at 6:23 pm

  9. Pretty cool!

    I just usually use this though:

    return ( !empty($foo) ) ? TRUE : FALSE;

    Viper007Bond

    October 11, 2007 at 8:46 pm

  10. [...] Doublebang Ever seen or written this PHP code? [...]

    Top Posts « WordPress.com

    October 12, 2007 at 1:20 am

  11. A programmer I see!!! I took two years of computer programming in my schooling. It helped me a lot, sometimes I would use the software to create simple games or to figure out my homework =]
    http://scott47.wordpress.com/

    scott47

    October 12, 2007 at 4:23 am

  12. Someone fancying an Obfuscated PHP Contest like Perl had? :)

    Ozh

    October 12, 2007 at 7:48 am

  13. @Aja
    More familiar to “Gangbang”? ;)

    n-blue

    October 12, 2007 at 10:39 am

  14. Nice.. fun :)

    rxbbx

    October 13, 2007 at 5:54 am

  15. @n-blue: Now, I know. ROFL

    @Viper007Bond: return !empty($foo); would actually produce the same result, the ternary op just makes it explicit and redundant. Besides, !! is still shorter than !empty(). :)

    Aja

    October 13, 2007 at 12:43 pm

  16. rxbbx: Now, now, “gang” suggest more than two … heh

    Mark Steel

    October 14, 2007 at 2:29 pm

  17. Err, oops. I meant n-blue. Sorry. ;-) (Of course, I’d throw a missing “s” up there if I could edit, too)

    Mark Steel

    October 14, 2007 at 2:30 pm

  18. i got here because i thought it referred to Max/MSP

    dlab

    October 16, 2007 at 4:07 pm

  19. … I was so disappointed…

    Maja

    October 17, 2007 at 2:50 pm

  20. I realize this is completely off-topic, but is it possible to order front page posts – last commented on top? And if it is possible, (there is no plugin-I checked) what would writing a this kind of plugin include?

    vlad

    October 23, 2007 at 1:26 am

  21. [...] Doublebang « Mark on WordPress (tags: php wordpress) Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]

  22. I don’t Ever saw this PHP code. This is very curious xP

    Gry Logiczne

    September 21, 2008 at 10:56 am


Leave a Reply