- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
Admin
This heralds the aPHPocalyPHPse!!!
Admin
But what is truth? Are mine the same as yours?
Admin
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!
Admin
Knowing the truth can never be false.
Admin
Interestingly, in PHP,
NaN
is truthy.Tell me honestly, is there in the world any sane reason to ever consider the truthiness of
NaN
?Admin
Ever since KelleyAnne Conway uttered "alternative facts" we've been living in a post-truth world.
Admin
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 finalelse
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 precedingelseif
block.Admin
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.
Admin
I have honestly seen some operations that return a true NaN.
Admin
I’m guessing that’s because in PHP, you can do things like
1+"1"
to get2
.Admin
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.
Admin
protected static function ill_have_what_shes_having( $value ) { // @codingStandardsIgnoreStart static $true = array( 'yes', 'Yes', 'YES', );
Admin
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.
Admin
Yes, many!
Here are the cases where NaN is non-true:
NaN == false
.NaN == false
.NaN == FILE_NOT_FOUND
NaN == false
.The cases where NaN is true are left as an exercise to the reader.
Admin
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.
Admin
Nicole Brown and Ron Goldman's relatives would definitely prefer the "alternative facts", don't you think?
Admin
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.
Admin
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:
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.)
Admin
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.
Admin
They've missed out the variations of the truth=beauty theme...
Admin
Very Meg Ryan...
Admin
Fair enough, but what about those people who held down the shift key just a little too long and wrote "TRue"?
Admin
I often realize the caps lock is on after typing about three letters, so "TRUe".
Admin
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".
Admin
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.
Admin
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!
Admin
All numeric values other than 1 are false. That's going to mess somebody up.
Admin
@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.
Admin
Do you honestly want a non-null native boolean response to that?
Admin
Anybody notice this:
--> 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.