• 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?

Floren

Well-known member
#1
I was wondering if is not "wiser" to use a proper Boolean usage of conditionals in XenForo:
Code:
$logoLink = ($options->logoLink ? $homeLink : XenForo_Link::buildPublicLink('full:index'));
Should be:
Code:
$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.
 
#3
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?
 

Floren

Well-known member
#4
$logoLink should be a URL, why are you getting boolean errors?
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.

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.
Not true.
 
#5
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:
PHP:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
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?
 

Jeremy

Well-known member
#7
Is stated into manual, cannot be clearer than this:

View attachment 21564
The manual also states this:
PHP Manual said:
Converting to boolean

To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.

See also Type Juggling.

When converting to boolean, the following values are considered FALSE:

  • the boolean FALSE itself
  • the integer 0 (zero)
  • the float 0.0 (zero)
  • the empty string, and the string "0"
  • an array with zero elements
  • an object with zero member variables (PHP 4 only)
  • the special type NULL (including unset variables)
  • SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
http://php.net/manual/en/language.types.boolean.php

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

Floren

Well-known member
#8
You are right. I guess, my brain got used to a strict check (PHP 5.4.0), in order to avoid possible issues:
Code:
$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.