Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Booleans compared to strings

11 views
Skip to first unread message

Doug Cassidy

unread,
May 13, 2013, 8:29:05 AM5/13/13
to
So, this is wierd:

$b = true;
var_dump($b);//boolean true

if(!$b)echo '!$b I wont echo, correctly so<BR>';
if($b)echo '$b I will echo, correctly so<BR>';

if($b == 'false')echo '$b == I will echo, which is wrong<BR>';
if($b === 'false')echo '$b === I wont echo, which is correct<BR>';

if($b == 'true')echo '$b == I will echo, which is kinda correct<BR>';
if($b === 'true')echo '$b === I wont echo, which is very correct<BR>';

Yes, I know that 'true' and 'false' are strings, not bool.

this one:
if($b == 'false')echo '$b == I will echo, which is wrong<BR>';

I dont see why boolean true is equal to string false in any way.

Jerry Stuckle

unread,
May 13, 2013, 9:13:11 AM5/13/13
to
A string comparison returns false only if the string is NULL, is an
empty string ('') or contains a zero ('0'). All other values (including
'false') are true.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

Robert Heller

unread,
May 13, 2013, 9:57:10 AM5/13/13
to
At Mon, 13 May 2013 05:29:05 -0700 (PDT) Doug Cassidy <do...@dougcassidy.com> wrote:

>
> So, this is wierd:
>
> $b = true;
> var_dump($b);//boolean true
>
> if(!$b)echo '!$b I wont echo, correctly so<BR>';
> if($b)echo '$b I will echo, correctly so<BR>';
>
> if($b == 'false')echo '$b == I will echo, which is wrong<BR>';
> if($b === 'false')echo '$b === I wont echo, which is correct<BR>';
>
> if($b == 'true')echo '$b == I will echo, which is kinda correct<BR>';
> if($b === 'true')echo '$b === I wont echo, which is very correct<BR>';
>
> Yes, I know that 'true' and 'false' are strings, not bool.

Well, duh!

>
> this one:
> if($b == 'false')echo '$b == I will echo, which is wrong<BR>';
>
> I dont see why boolean true is equal to string false in any way.

Any non empty value (including a non empty string) is true, therefore "$b ==
'false'" evaluates to true. Live with it.

PHP is using the *C* convention with respect to booleans. 0 is false, and any
non-zero value is true. A NULL pointer (eg an empty string) is effectively the
same as a numeric value of 0, and is thus false also. Any non-NULL pointer (eg
any object, array, or string) is effectively the same as a non 0 numeric
value, and is thus true.

Why in the world are you comparing booleans to strings? If this is a
string-based parameter setting, you should be just comparing the strings and
not be using booleans at all. Or you should not be using string-based
parameter settings for boolean parameters. Or you need to write a suitable
checking function that does an 'intellegent' type casting that suits the use
case you are handling. Something like:

function string_to_boolean($string) {
switch ($string) {
case 'true':
case 'yes':
case '1':
case 1:
case true:
return true;
case 'false':
case 'no':
case '0':
case 0:
case false:
return false;
default:
if (empty($string)) {
return false;
} else {
return true;
}
}
}

Then:

if ($b == string_to_boolean('false')) echo '$b == string_to_boolean(\'false\') I wont echo, which is correct<BR>';

Will behave as expected.


--
Robert Heller -- 978-544-6933 / hel...@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments



Thomas 'PointedEars' Lahn

unread,
May 13, 2013, 11:18:18 AM5/13/13
to
Doug Cassidy wrote:

> So, this is wierd:

“Weird” is a subjective assessment.
It's not a bug, it's a feature. See
<http://php.net/manual/en/language.types.type-juggling.php> for details.
By contrast to “==” and “!=”, “===” and “!==” do not do type juggling.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Thomas 'PointedEars' Lahn

unread,
May 13, 2013, 11:22:54 AM5/13/13
to
Jerry Stuckle wrote:

> On 5/13/2013 8:29 AM, Doug Cassidy wrote:
>> So, this is wierd:
>>
>> $b = true;
>> var_dump($b);//boolean true
>>
>> if(!$b)echo '!$b I wont echo, correctly so<BR>';
>> if($b)echo '$b I will echo, correctly so<BR>';
>>
>> if($b == 'false')echo '$b == I will echo, which is wrong<BR>';
>> if($b === 'false')echo '$b === I wont echo, which is correct<BR>';
>>
>> if($b == 'true')echo '$b == I will echo, which is kinda correct<BR>';
>> if($b === 'true')echo '$b === I wont echo, which is very correct<BR>';
>>
>> Yes, I know that 'true' and 'false' are strings, not bool.
>>
>> this one:
>> if($b == 'false')echo '$b == I will echo, which is wrong<BR>';
>>
>> I dont see why boolean true is equal to string false in any way.
>
> A string comparison returns false only if the string is NULL, is an
> empty string ('') or contains a zero ('0'). All other values (including
> 'false') are true.

The reason for this is type juggling, not “string comparison”.

What you might have meant is how a string value in a type-converting
conditional expression is evaluated (like that of “if ($string)” or
“($string) ? … : …”), where type juggling is performed as well.

Doug Cassidy

unread,
May 13, 2013, 4:23:42 PM5/13/13
to
On Monday, May 13, 2013 6:57:10 AM UTC-7, Robert Heller wrote:

> Why in the world are you comparing booleans to strings?

Dealing with mistakenly written legacy code.

Doug Cassidy

unread,
May 13, 2013, 4:24:32 PM5/13/13
to
On Monday, May 13, 2013 6:13:11 AM UTC-7, Jerry Stuckle wrote:
> On 5/13/2013 8:29 AM, Doug Cassidy wrote:

> > I dont see why boolean true is equal to string false in any way.

> A string comparison returns false only if the string is NULL, is an
> empty string ('') or contains a zero ('0'). All other values (including
> 'false') are true.

got it, thanks

M. Strobel

unread,
May 13, 2013, 5:22:13 PM5/13/13
to
My favourite link to comparison in PHP is
http://www.php.net/manual/en/types.comparisons.php
where you have also the "if ($x)" results.

Highly recommended.

/Str.

Thomas 'PointedEars' Lahn

unread,
May 13, 2013, 5:46:35 PM5/13/13
to
Slighty off, though, because “if” is _not_ a function.


PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286...@94.75.214.39>

The Natural Philosopher

unread,
May 13, 2013, 6:21:48 PM5/13/13
to
On 13/05/13 16:18, Thomas 'PointedEars' Lahn wrote:
> Doug Cassidy wrote:
>
>> So, this is wierd:
> “Weird” is a subjective assessment.
even when spelt* correctly

* that is a valid and in many ways more correct version of 'spelled'

>> $b = true;
>> var_dump($b);//boolean true
>>
>> if(!$b)echo '!$b I wont echo, correctly so<BR>';
>> if($b)echo '$b I will echo, correctly so<BR>';
>>
>> if($b == 'false')echo '$b == I will echo, which is wrong<BR>';
>> if($b === 'false')echo '$b === I wont echo, which is correct<BR>';
>>
>> if($b == 'true')echo '$b == I will echo, which is kinda correct<BR>';
>> if($b === 'true')echo '$b === I wont echo, which is very correct<BR>';
>>
>> Yes, I know that 'true' and 'false' are strings, not bool.
>>
>> this one:
>> if($b == 'false')echo '$b == I will echo, which is wrong<BR>';
>>
>> I dont see why boolean true is equal to string false in any way.
> It's not a bug, it's a feature. See
> <http://php.net/manual/en/language.types.type-juggling.php> for details.
> By contrast to “==” and “!=”, “===” and “!==” do not do type juggling.

Thats the trouble with a loosely typed language that trys to second
guess what you mean. You cant second guess total iditiots. And ometimes
you may fail to correctly guess wise men too.

ISTR the bug i found where two browsers gave entirely different results
in javascript was more or less of the < if ('1' == 1) > variety. One
browser said true, the other said false...and when I looked at the
javsacript spec there seemd to be no casting rule defined for conditionals.

And I had to spend a day trying to force an explicit cast so the same
code produced the same result in firefox as IE6.


>
> PointedEars


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to lead are elected by the least capable of producing, and where the members of society least likely to sustain themselves or succeed, are rewarded with goods and services paid for by the confiscated wealth of a diminishing number of producers.

The Natural Philosopher

unread,
May 13, 2013, 6:26:26 PM5/13/13
to
Yuk I do not like "0" being a representation of FALSE at all.

> Highly recommended.
>
> /Str.

The Natural Philosopher

unread,
May 13, 2013, 6:28:55 PM5/13/13
to
On 13/05/13 22:46, Thomas 'PointedEars' Lahn wrote:
> M. Strobel wrote:
>
>> Am 13.05.2013 22:24, schrieb Doug Cassidy:
>>> On Monday, May 13, 2013 6:13:11 AM UTC-7, Jerry Stuckle wrote:
>>>> On 5/13/2013 8:29 AM, Doug Cassidy wrote:
>>>>> I dont see why boolean true is equal to string false in any way.
>>>> A string comparison returns false only if the string is NULL, is an
>>>> empty string ('') or contains a zero ('0'). All other values (including
>>>> 'false') are true.
>>> got it, thanks
>> My favourite link to comparison in PHP is
>> http://www.php.net/manual/en/types.comparisons.php
>> where you have also the "if ($x)" results.
>>
>> Highly recommended.
> Slighty off, though, because “if” is _not_ a function.
Semantics. It behaves like one, takes two variables and returns a value.

In FORTH its like any other function IIRC, But then everything is a
function , in forth :-)
>
> PointedEars

Thomas 'PointedEars' Lahn

unread,
May 13, 2013, 6:41:31 PM5/13/13
to
The Natural Philosopher wrote:

> On 13/05/13 22:46, Thomas 'PointedEars' Lahn wrote:
>> M. Strobel wrote:
>>> Am 13.05.2013 22:24, schrieb Doug Cassidy:
>>>> On Monday, May 13, 2013 6:13:11 AM UTC-7, Jerry Stuckle wrote:
>>>>> On 5/13/2013 8:29 AM, Doug Cassidy wrote:
>>>>>> I dont see why boolean true is equal to string false in any way.
>>>>> A string comparison returns false only if the string is NULL, is an
>>>>> empty string ('') or contains a zero ('0'). All other values
>>>>> (including 'false') are true.
>>>> got it, thanks
>>> My favourite link to comparison in PHP is
>>> http://www.php.net/manual/en/types.comparisons.php
>>> where you have also the "if ($x)" results.
>>>
>>> Highly recommended.
>> Slighty off, though, because “if” is _not_ a function.
> Semantics. It behaves like one, takes two variables and returns a value.

I am not usually reading your postings because of your notorious address
munging and anonymous posting, but really …

What the *heck* are you talking about? An “if” *statement* does not return
anything (in neither programming language where it is a statement), and it
does not “take two variables”. It is the *one* “parameter” (for lack of a
better word) to the statement that is evaluated to a value, which is then
converted to boolean, which then determines whether or not the adjacent
statement is executed.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Jerry Stuckle

unread,
May 13, 2013, 6:56:27 PM5/13/13
to
On 5/13/2013 6:28 PM, The Natural Philosopher wrote:
> On 13/05/13 22:46, Thomas 'PointedEars' Lahn wrote:
>> M. Strobel wrote:
>>
>>> Am 13.05.2013 22:24, schrieb Doug Cassidy:
>>>> On Monday, May 13, 2013 6:13:11 AM UTC-7, Jerry Stuckle wrote:
>>>>> On 5/13/2013 8:29 AM, Doug Cassidy wrote:
>>>>>> I dont see why boolean true is equal to string false in any way.
>>>>> A string comparison returns false only if the string is NULL, is an
>>>>> empty string ('') or contains a zero ('0'). All other values
>>>>> (including
>>>>> 'false') are true.
>>>> got it, thanks
>>> My favourite link to comparison in PHP is
>>> http://www.php.net/manual/en/types.comparisons.php
>>> where you have also the "if ($x)" results.
>>>
>>> Highly recommended.
>> Slighty off, though, because “if” is _not_ a function.
> Semantics. It behaves like one, takes two variables and returns a value.
>
> In FORTH its like any other function IIRC, But then everything is a
> function , in forth :-)
>>

Who cares about forth? This is a PHP newsgroup.

But then you don't know the difference.

Jerry Stuckle

unread,
May 13, 2013, 6:57:43 PM5/13/13
to
On 5/13/2013 6:26 PM, The Natural Philosopher wrote:
> On 13/05/13 22:22, M. Strobel wrote:
>> Am 13.05.2013 22:24, schrieb Doug Cassidy:
>>> On Monday, May 13, 2013 6:13:11 AM UTC-7, Jerry Stuckle wrote:
>>>> On 5/13/2013 8:29 AM, Doug Cassidy wrote:
>>>>> I dont see why boolean true is equal to string false in any way.
>>>> A string comparison returns false only if the string is NULL, is an
>>>> empty string ('') or contains a zero ('0'). All other values
>>>> (including
>>>> 'false') are true.
>>> got it, thanks
>>>
>> My favourite link to comparison in PHP is
>> http://www.php.net/manual/en/types.comparisons.php
>> where you have also the "if ($x)" results.
>
> Yuk I do not like "0" being a representation of FALSE at all.
>

Then create your own language. Or live with what the language defines.

M. Strobel

unread,
May 14, 2013, 3:36:17 PM5/14/13
to
Am 14.05.2013 00:28, schrieb The Natural Philosopher:
> On 13/05/13 22:46, Thomas 'PointedEars' Lahn wrote:
>> M. Strobel wrote:
>>
>>> Am 13.05.2013 22:24, schrieb Doug Cassidy:
>>>> On Monday, May 13, 2013 6:13:11 AM UTC-7, Jerry Stuckle wrote:
>>>>> On 5/13/2013 8:29 AM, Doug Cassidy wrote:
>>>>>> I dont see why boolean true is equal to string false in any way.
>>>>> A string comparison returns false only if the string is NULL, is an
>>>>> empty string ('') or contains a zero ('0'). All other values (including
>>>>> 'false') are true.
>>>> got it, thanks
>>> My favourite link to comparison in PHP is
>>> http://www.php.net/manual/en/types.comparisons.php
>>> where you have also the "if ($x)" results.
>>>
>>> Highly recommended.
>> Slighty off, though, because “if” is _not_ a function.
> Semantics. It behaves like one, takes two variables and returns a value.
>
> In FORTH its like any other function IIRC, But then everything is a function , in
> forth :-)

Hmm, in Forth you do 0= or something, then "if" takes a flag off the stack and does a
branch/jump to the else address if the flag is false.

Not easy to see it as a function.

/Str.

The Natural Philosopher

unread,
May 14, 2013, 4:17:32 PM5/14/13
to
try writing the FORTH interpreter...
> /Str.

M. Strobel

unread,
May 15, 2013, 3:34:32 PM5/15/13
to
No problem, did it in /370 assembly. You mean because it is an immediate word? but it
is not a function, it does not return a value, only side effects.

/Str.

0 new messages