1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Boolean usage of conditionals in XenForo?

Discussion in 'XenForo Development Discussions' started by Floren, Nov 19, 2011.

  1. Floren

    Floren Well-Known Member

    I was wondering if is not "wiser" to use a proper Boolean usage of conditionals in XenForo:
    $logoLink = ($options->logoLink ? $homeLink : XenForo_Link::buildPublicLink('full:index'));
    Should be:
    $logoLink = (bool) $options->logoLink
    	? $homeLink
    	: XenForo_Link::buildPublicLink('full:index');
    This will avoid all warnings spit by PHP, if your variable is not truly Boolean.
  2. Jeremy

    Jeremy Well-Known Member

    $logoLink should be a URL, why are you getting boolean errors?
  3. CJ.Wurtz

    CJ.Wurtz Member

    I can't see that giving a warning (or even a notice). Any type of data should have a valid result. Strings will be true if set, arrays true if they have any elements, integers if their value is >= 1. Even if the "logoLink" property didn't exist in the $options Object, it would still just throw a notice. What warning have you seen thrown from that code?
  4. Floren

    Floren Well-Known Member

    Kier/Mike use a ternary operator, which is solely based on a boolean only input. Is equivalent to:
    IF expr1 === true THEN alpha ELSE beta;

    Right now $options->logoLink returns a string and creates a warning in PHP, as it expects a boolean value instead.
    A simple var_dump() will tell you exactly the output, it will return a string instead of BOOL which is expected within a ternary operator.

    Not true.
  5. CJ.Wurtz

    CJ.Wurtz Member

    Huh? The only thing in my statement that was inaccurate was "string will be true if set", I didn't mean it as a set/unset as much as not empty ("0" having slipped my mind).
    But PHP doesn't need these explicitly cast as a boolean, it does that conversion itself. And I've never seen it give any error whatsoever because of that.

    I double checked for the sake of my sanity and this:
    echo (
    "http://someurl.com/logo.png" "TRUE" "FALSE");
    Does not throw any warning for me.

    I can't help but ask, exactly what warning are you seeing from the code snippet you posted?
  6. Floren

    Floren Well-Known Member

    Is stated into manual, cannot be clearer than this:

  7. Jeremy

    Jeremy Well-Known Member

    The manual also states this:

    I too am failing to see how you are getting any type of boolean error.
  8. Floren

    Floren Well-Known Member

    You are right. I guess, my brain got used to a strict check (PHP 5.4.0), in order to avoid possible issues:
    $var = 'text';
    var_dump(0 == $var); // true, which should be false
    var_dump((bool) 0 == $var); // false
    Personally, I always return the expression properly evaluated. If PHP expects an expression to eval'ed TRUE/FALSE, I make sure it gets that from the start.

Share This Page