Re: TRUE and FALSE are treated differently



A Bit Narked wrote:
On Tue, 08 Jan 2008 08:00:23 -0500,
Jerry Stuckle <jstucklex@xxxxxxxxxxxxx> wrote:

A Bit Narked wrote:
On Sun, 6 Jan 2008 23:19:45 +0000,
Toby A Inkster <usenet200712@xxxxxxxxxxxxxxxxx> wrote:

A Bit Narked wrote:

And yet you can echo true. The output is '1'.
No -- you can't echo a boolean in PHP. Why you try, the boolean is implicitly cast to a string.

This:

echo $bool;

is actually executed as if you coded this:

echo (string)$bool;

If you look up the manual on type casting, you'll find that FALSE is cast to an empty string, and TRUE is cast to a non-empty string.

If you cast to an integer instead of a string, then you'll find that FALSE is cast to 0, which seems to be what you want.

echo (int)FALSE;
Apologies for imprecise expression :-) PHP provides free
casting (e.g. to string in echo) so I ignore it.

My real point is that there is no logical relationship between a
boolean value of TRUE and a string value '1' _except_ where the
same relationship yields a string value '0' for boolean FALSE.
I.e., the C precedent.

I'm not fashed by the idea that there's a special boolean type
whose values are TRUE and FALSE. But logically, the string
representations of such sui generis values should be 'true' and
'false'. There is no logic whatever in string representions of
'1' and ''. No one has articulated a relationship such that
'1' and '' makes any sort of sense.

One of the basic principles of good design is predictability.
'From one thing know ten thousand things' as the Chinese stated
it ages ago. So if there's a well-known precedent, we should
follow it unless we have a powerful reason for not doing ('Ensure
that everyone after us makes the same mistake we did', as Maurice
Wilkes put it, to appreciative laughter). And whatever we do
should be internally consistent. So the calling sequence for
some new function should be guessable by anyone who knows the
sequences of similar functions, one value should tell us about
neighboring values, and so forth.

The PHP developers' choice for the string reps of true and false
violate this principle all to buggery, which is what got up my
nostril.

I thought everyone here would immediately see how daft it is, but
I expect we're circling the drain as a species precisely because
so many people -even very smart ones- go through life accepting
without examination that 'whatever is, is right'.

Sorry, but it makes perfect sense.

C does not have real boolean values "true" or "false". Rather, it gimmicks them up by saying anything with a non-zero value is true, and anything with a zero value is false. One of the problems is that both 1 and 2 are considered true - but in this case true != true (1 != 2).

PHP does have a real boolean "type" with the values "true" and "false". So true == true all the time.

PHP can also do conversions. Like with C, anything with a non-zero value is true, and anything with a zero value is false. This includes an empty string.

And if we kept doing things the same old way, we'd still be rubbing sticks together to keep the cave warm. Progress always means change.

Progress is a type of change, but change is not a type of
progess.

This seems to be a very easy error to make. You've made it 3
times in your reply!


Not at all. I explained exactly how it works.

If you don't like it, no one is forcing you to use PHP.

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

.



Relevant Pages

  • Re: TRUE and FALSE are treated differently
    ... No -- you can't echo a boolean in PHP. ... the boolean is implicitly cast to a string. ... If you look up the manual on type casting, you'll find that FALSE is cast to an empty string, and TRUE is cast to a non-empty string. ...
    (comp.lang.php)
  • Re: TRUE and FALSE are treated differently
    ... No -- you can't echo a boolean in PHP. ... If you look up the manual on type casting, you'll find that FALSE is cast ... to an empty string, and TRUE is cast to a non-empty string. ...
    (comp.lang.php)
  • Re: php form info...
    ... when trying to search in the mysql statement. ... : echo $testvar; ... combine a variable containing an arbitrary string with a constant number. ... The results within php depend a great deal on the contents ...
    (comp.lang.php)
  • Re: How does PHP store strings?
    ... To access the 'null terminator', ... or access out of the bounds of the string. ... I tried doing this in PHP 5 by writing: ... echo $string; ...
    (php.general)
  • Re: if (!0) means what?
    ... Boolean is a distinct type in PHP. ... echo "I am false"; ... echo "I am not zero"; ...
    (comp.lang.php)