• (nodebb)
    $frist = array(1, 'frist', 'first');
  • I must be doing it wrong then (unregistered)
         * Tests if a given string is acceptable as a <code>BOOLEAN</code> and returns it
         * This is a more cromulent version of the PHP Core Methods
         * <i>filter_var($variable, FILTER_VALIDATE_BOOLEAN)</i> and <i>boolval()</i>
         * in that it has a wider and more specific range of "candidates" to
         * check against. It is particularly useful for HTML Form Data.
         * @note The returned value should be checked for <code>NULL</code>
         * @param scalar $inputVariable The variable to be tested / coerced
         * @param boolean $asText [optional, defaults to FALSE] If present and is not FALSE it will return a textual representation
         * @return mixed <i>BOOLEAN</i> <code>TRUE</code> or <code>FALSE</code> or <code>NULL</code> accordingly.<br>
         *               If <i><b>parameter</b> $asText</i> is <code>TRUE</code> then,<br>
         *               "*TRUE*" or "*FALSE*" or "*NULL*" respectively.
        public static function GetBoolean($inputVariable, $asText=false) {
            $methodReturn = null;
            $possibleText = array(
                'true' => array('t', 'true', 'y', 'yes', 'on', 's', 'set', 'h', 'high',),
                'false' => array('f','false', 'n', 'no', 'off', 'c', 'clear', 'l', 'low',),
            if ($asText !== false) { $asText = true; }
            if (is_null($inputVariable)) { $methodReturn = null; }
            else if (($inputVariable === true) || ($inputVariable === false)) { $methodReturn = $inputVariable; }
            else if (is_scalar($inputVariable)) {
                if (is_string($inputVariable)) {
                    $inputVariable = strtolower($inputVariable);
                    if (in_array($inputVariable, $possibleText['true'])) { $methodReturn  = true; }
                    else if (in_array($inputVar
  • Sauron (unregistered)

    This heralds the aPHPocalyPHPse!!!

  • TheCPUWizard (unregistered)

    But what is truth? Are mine the same as yours?

  • (nodebb)

    This reminds me a little of the coders who moved from Pascal to C and used: #define begin { #define end }

    If you are going to use a language, use that language!

  • (nodebb)

    Knowing the truth can never be false.

  • Sauron (unregistered)

    Interestingly, in PHP, NaN is truthy.

    Tell me honestly, is there in the world any sane reason to ever consider the truthiness of NaN ?

  • (nodebb) in reply to TheCPUWizard

    Ever since KelleyAnne Conway uttered "alternative facts" we've been living in a post-truth world.

  • (nodebb)

    I'm not bothered too much about not enumerating all the possible capitalizations. Anyone who writes tRue gets what they deserve.

    Lots of config files allow a variety of ways to turn an option on or off. You can write optionName=1, optionName=on, optionName=true, etc. That's presumably what the two arrays are for. Using the $false array allows for the easy change of the final else block to report a syntax error for unrecognized values. Maybe it did at one time, but they changed it and didn't bother to remove the preceding elseif block.

  • Smithers (unregistered) in reply to Sauron

    I don't know; is idle curiosity a sane reason?

    The question of my sanity aside, it turns out that both C++ and Python also convert NaN to boolean true. JavaScript actually seems to be the oddball here, treating it as falsey.

  • Mr Bits (unregistered) in reply to Sauron

    Interestingly, in PHP, NaN is truthy.

    Tell me honestly, is there in the world any sane reason to ever consider the truthiness of NaN ?

    I have honestly seen some operations that return a true NaN.

  • (nodebb)

    The string "0" makes my skin itch a bit

    I’m guessing that’s because in PHP, you can do things like 1+"1" to get 2.

  • (nodebb)

    I had a website once that said my phone number was invalid. Some investigation showed that having my phone number end in a zero caused the problem. I blame PHP.

  • Meir (unregistered)

    protected static function ill_have_what_shes_having( $value ) { // @codingStandardsIgnoreStart static $true = array( 'yes', 'Yes', 'YES', );

  • Randal L. Schwartz (github)

    This actually mimics PHP's inspiration, Perl fairly closely. Not surprising, since the first PHP was a big Perl app and PHP continued to "borrow" from Perl for most of its development.

  • (nodebb) in reply to Sauron

    Tell me honestly, is there in the world any sane reason to ever consider the truthiness of NaN ?

    Yes, many!

    Here are the cases where NaN is non-true:

    • The kitchen might be out of NaN dough, or their tandoor oven might be broken. So NaN == false.
    • You might have a dish of butter chicken in front of you, but a platter of rice to go along with it. So NaN == false.
    • The restaurant order site might not list NaN as available. So NaN == FILE_NOT_FOUND
    • You might forget to specify NaN. So NaN == false.

    The cases where NaN is true are left as an exercise to the reader.

  • (nodebb)

    Note the reference to checkboxes. I suspect this is looking at checkbox text, not user input or the like. They had a third path that yapped, found all the variants that were in use, then removed the yap. It's easy to resurrect, though, if a problem emerges in the future. And I have no idea of what debugging capabilities might exist but the third path could be used for a breakpoint even as the code stands.

    It's also sane when reading a config file--tolerates a wide variety of what humans might do. But yap if it's none of the above!

    The only real sin I see here is the case logic.

  • (nodebb) in reply to Barry Margolin

    Nicole Brown and Ron Goldman's relatives would definitely prefer the "alternative facts", don't you think?

  • Bruce (unregistered) in reply to LorenPechtel

    Loren, could you please given details on the sin of the case logic? e.g. is it specific such as not converting $value to an int for the comparisons or more general.

  • (nodebb)

    This will probably be read by nobody, but anyway, I had reason some years ago to do some string-to-bool conversions of largish numbers of human entered values. This is the result. It may not be perfect (indeed, there is no perfect answer), but it did the job needed at the time, and is better than the WTF presented:

           else if (targetType == typeof(bool))
               var lowerString = stringValue.ToLowerInvariant();
               switch (lowerString)
                   case "true":
                   case "yes":
                   case "1":
                   case "t":
                   case "y":
                   case "+":
                   case "on":
                       convertedValue = (T)(object)true;
                       return true;
                   case "false":
                   case "no":
                   case "0":
                   case "-1":
                   case "f":
                   case "n":
                   case "-":
                   case "off":
                       convertedValue = (T)(object)false;
                       return true;
                       convertedValue = (T)(object)(lowerString.StartsWith("t") || lowerString.StartsWith("y"));
                       return false;

    Addendum 2024-04-12 01:09: (convertedValue is the bool it is going with. The actual function's return value can be thought of as a kind of confidence that it is more than likely correct.)

  • Nick (unregistered) in reply to The Beast in Black

    While I see what you’re going for, that seems like a lot of effort for a simple joke about “NaN” being similar to the word for an Indian flatbread, or Naan.

    You didn’t think the bread was a Nan, did you?

    In my case, my Nan is currently 103 (she’ll be 104 later this month), so I would say Nan is very much true.

  • Darren (unregistered)

    They've missed out the variations of the truth=beauty theme...

  • Clive (unregistered)

    Very Meg Ryan...

  • (nodebb) in reply to Barry Margolin

    Anyone who writes tRue gets what they deserve.

    Fair enough, but what about those people who held down the shift key just a little too long and wrote "TRue"?

  • markm (unregistered) in reply to jkshapiro

    I often realize the caps lock is on after typing about three letters, so "TRUe".

  • John (unregistered)

    I can't be the only person in the world who hates designing tables and index to start at one because some stupid language feature thinks that a zero is "false".

  • Richard (unregistered)

    I feel the pain regarding checkboxes.

    HTML's form, "input type=checkbox name=myinput value=myval" does something awful when not checked.

    When it is checked, POST contains "myinput=myval".
    When not checked, POST contains "myinput=''" . or so one might think. But oh, no it doesn't. POST actually omits the variable entirely!

    As for PHP treating "0" as falsey - there may have been a good historic reason for it, but it's really stupid - the fact that it is a "non-empty string" should be more important than the fact that its integer value is zero.

  • (nodebb) in reply to Nick

    I was actually going for "NāN" i.e the IAST (International Alphabet of Sanskrit Transliteration, not "interactive application security testing") representation of the transliteration of the Indian word - which uses diacritics - but was too lazy to go lookup and copy-paste the "ā" character :)

    Also, congrats to your Nan for breaking the century mark!

  • Andrew Klossner (unregistered)
    Comment held for moderation.
  • (nodebb)

    @Bruce: My problem with it is that it should have been forced to either all uppercase or all lowercase rather than the mess we see. I also like y6ird's approach.

    Beyond that, I think this is just a response to inconsistent input.

  • DeeKay (unregistered) in reply to Sauron
    Comment held for moderation.
  • Shorty (unregistered)

    Anybody notice this:

    elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) {
    	return (bool) $value;
    } elseif ...

    --> elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) { return (bool) $value; } elseif ...

    The code after && is not part of the elseif condition.

Leave a comment on “To Tell the Truth”

Log In or post as a guest

Replying to comment #:

« Return to Article