Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9289 > unrolled thread

Booleans compared to strings

Started byDoug Cassidy <doug@dougcassidy.com>
First post2013-05-13 05:29 -0700
Last post2013-05-13 23:21 +0100
Articles 18 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  Booleans compared to strings Doug Cassidy <doug@dougcassidy.com> - 2013-05-13 05:29 -0700
    Re: Booleans compared to strings Jerry Stuckle <jstucklex@attglobal.net> - 2013-05-13 09:13 -0400
      Re: Booleans compared to strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-13 17:22 +0200
      Re: Booleans compared to strings Doug Cassidy <doug@dougcassidy.com> - 2013-05-13 13:24 -0700
        Re: Booleans compared to strings "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-05-13 23:22 +0200
          Re: Booleans compared to strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-13 23:46 +0200
            Re: Booleans compared to strings The Natural Philosopher <tnp@invalid.invalid> - 2013-05-13 23:28 +0100
              Re: Booleans compared to strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-14 00:41 +0200
              Re: Booleans compared to strings Jerry Stuckle <jstucklex@attglobal.net> - 2013-05-13 18:56 -0400
              Re: Booleans compared to strings "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-05-14 21:36 +0200
                Re: Booleans compared to strings The Natural Philosopher <tnp@invalid.invalid> - 2013-05-14 21:17 +0100
                  Re: Booleans compared to strings "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2013-05-15 21:34 +0200
          Re: Booleans compared to strings The Natural Philosopher <tnp@invalid.invalid> - 2013-05-13 23:26 +0100
            Re: Booleans compared to strings Jerry Stuckle <jstucklex@attglobal.net> - 2013-05-13 18:57 -0400
    Re: Booleans compared to strings Robert Heller <heller@deepsoft.com> - 2013-05-13 08:57 -0500
      Re: Booleans compared to strings Doug Cassidy <doug@dougcassidy.com> - 2013-05-13 13:23 -0700
    Re: Booleans compared to strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-13 17:18 +0200
      Re: Booleans compared to strings The Natural Philosopher <tnp@invalid.invalid> - 2013-05-13 23:21 +0100

#9289 — Booleans compared to strings

FromDoug Cassidy <doug@dougcassidy.com>
Date2013-05-13 05:29 -0700
SubjectBooleans compared to strings
Message-ID<b292b42b-16ed-40bc-b005-79970a9e6362@googlegroups.com>
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.

[toc] | [next] | [standalone]


#9293

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-05-13 09:13 -0400
Message-ID<kmqom5$q5m$1@dont-email.me>
In reply to#9289
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.


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

[toc] | [prev] | [next] | [standalone]


#9301

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-13 17:22 +0200
Message-ID<18034460.758D1zofqd@PointedEars.de>
In reply to#9293
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.


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

[toc] | [prev] | [next] | [standalone]


#9311

FromDoug Cassidy <doug@dougcassidy.com>
Date2013-05-13 13:24 -0700
Message-ID<2bd61219-9d95-458a-938c-24938fa4ab59@googlegroups.com>
In reply to#9293
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

[toc] | [prev] | [next] | [standalone]


#9313

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2013-05-13 23:22 +0200
Message-ID<avd3s5Fdr9lU1@mid.uni-berlin.de>
In reply to#9311
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.

/Str.

[toc] | [prev] | [next] | [standalone]


#9314

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-13 23:46 +0200
Message-ID<18508838.qTh01YhHoi@PointedEars.de>
In reply to#9313
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.


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:Xns9FB6521286DB8invalidcom@94.75.214.39>

[toc] | [prev] | [next] | [standalone]


#9317

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-05-13 23:28 +0100
Message-ID<kmrpf7$ejd$2@news.albasani.net>
In reply to#9314
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


-- 
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.

[toc] | [prev] | [next] | [standalone]


#9320

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-14 00:41 +0200
Message-ID<1474783.kRIWQ1k3cJ@PointedEars.de>
In reply to#9317
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

[toc] | [prev] | [next] | [standalone]


#9321

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-05-13 18:56 -0400
Message-ID<kmrqrn$b7u$1@dont-email.me>
In reply to#9317
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.

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

[toc] | [prev] | [next] | [standalone]


#9327

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2013-05-14 21:36 +0200
Message-ID<avfi1iFudglU1@mid.uni-berlin.de>
In reply to#9317
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.

[toc] | [prev] | [next] | [standalone]


#9328

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-05-14 21:17 +0100
Message-ID<kmu64s$bn1$4@news.albasani.net>
In reply to#9327
On 14/05/13 20:36, M. Strobel wrote:
> 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.
try writing the FORTH interpreter...
> /Str.
>


-- 
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.

[toc] | [prev] | [next] | [standalone]


#9361

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2013-05-15 21:34 +0200
Message-ID<avi6a8Fhq1iU1@mid.uni-berlin.de>
In reply to#9328
Am 14.05.2013 22:17, schrieb The Natural Philosopher:
> On 14/05/13 20:36, M. Strobel wrote:
>> 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.
> try writing the FORTH interpreter...
>> /Str.
>>
> 
> 
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.

[toc] | [prev] | [next] | [standalone]


#9316

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-05-13 23:26 +0100
Message-ID<kmrpai$ejd$1@news.albasani.net>
In reply to#9313
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.

> Highly recommended.
>
> /Str.


-- 
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.

[toc] | [prev] | [next] | [standalone]


#9322

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-05-13 18:57 -0400
Message-ID<kmrqu3$b7u$2@dont-email.me>
In reply to#9316
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.



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

[toc] | [prev] | [next] | [standalone]


#9294

FromRobert Heller <heller@deepsoft.com>
Date2013-05-13 08:57 -0500
Message-ID<Au2dndEt9pCrbA3MnZ2dnUVZ_r6dnZ2d@giganews.com>
In reply to#9289
At Mon, 13 May 2013 05:29:05 -0700 (PDT) Doug Cassidy <doug@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 / heller@deepsoft.com
Deepwoods Software        -- http://www.deepsoft.com/
()  ascii ribbon campaign -- against html e-mail
/\  www.asciiribbon.org   -- against proprietary attachments


                                                              

[toc] | [prev] | [next] | [standalone]


#9310

FromDoug Cassidy <doug@dougcassidy.com>
Date2013-05-13 13:23 -0700
Message-ID<e235a1e8-a758-4d1e-b0cc-aa4157034e62@googlegroups.com>
In reply to#9294
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.

[toc] | [prev] | [next] | [standalone]


#9300

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-05-13 17:18 +0200
Message-ID<22836064.xOeTbpBGaj@PointedEars.de>
In reply to#9289
Doug Cassidy wrote:

> So, this is wierd:

“Weird” is a subjective assessment.

> $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.


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

[toc] | [prev] | [next] | [standalone]


#9315

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-05-13 23:21 +0100
Message-ID<kmrp1t$e40$1@news.albasani.net>
In reply to#9300
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web