# TRUE and FALSE are treated differently

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Can someone explain the rationale - if there is one - behind
treating the constants TRUE and FALSE differently?

echo false ;
echo false+false ;
echo intval(false) ;
echo '"'.false.'"' ;

echo true ;
echo true+true ;
echo intval(true) ;
echo '"'.true.'"' ;

should produce

000"0"121"1"

00""121"1"

In other words, the only way to output the underlying zero or use
it in a string is to use 'false+false' or pass it through
intval().  No such tricks are required to get at the 1 that
underlies true.

The whole idea of symbolic constants is that they are always
replaced by the underlying value during translation, and thus
*anywhere* you would otherwise have to use some obscure "magic
number" such as 191, you can use a symbolic constant that makes
sense in your context, such as TOTAL_NATIONS.

Exactly what we get -or php gets- out of php breaking this rule
was not explained to me by the person who marked my bug report
'bogus'.  Can someone here explain it?

## Re: TRUE and FALSE are treated differently

What I get are the constants/terms/reserved words true and false with
those values. What else do I need? There is no need to know what values
these equate to, if any.

## Re: TRUE and FALSE are treated differently

wrote:

Will return nothing (empty string) : ''

Will return 0 (cast to integer): '0';

Will return 0 (cast to integer): '00';

Will return '""': '00""';

Will return 1: '00""1';

Will return 2: '00""12';

Wil return 1: '00""121';

Will return '"1"': '00""121"1"';

No it shouldn't, echo false will not give you a zero. Take a hard look at
the types section in the manual, especially at type juggling.

--
Rik Wasmus

## Re: TRUE and FALSE are treated differently

wrote:

Ah, taking a closer look at your post, I'd say this: you cannot echo true
or false, they have no string value. PHP has loose typing, and false cast
to string would be '', cast to integer would be 0, the fact is that the
underlying value of false is not 0 (yes, it would be in C probably, but
not from the PHP side of things).
--
Rik Wasmus

## Re: TRUE and FALSE are treated differently

On Sun, 06 Jan 2008 19:43:28 +0100,

And yet you can echo true.  The output is '1'.  You can also
concat it into a string, where it also becomes a 1.  The two
constants are treated completely differently in that respect.

## Re: TRUE and FALSE are treated differently

.oO(A Bit Narked)

Nope. Both are just converted to a string:

TRUE  => non-empty string ('1', but it could be anything)
FALSE => empty string

Makes perfect sense.

Micha

## Re: TRUE and FALSE are treated differently

e:

=

=

the
but

AH, for some reason, can't see the previous post Michael comments on, bu=
t  =

indeed, echo will convert any variable to string it recieves. That's for=
=

instance how it's possible to define a __toString() method on an object.=

-- =

Rik Wasmus

## Re: TRUE and FALSE are treated differently

A Bit Narked wrote:

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;

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 7 days, 10:29.]

dhyana.pl/0.3
http://tobyinkster.co.uk/blog/2008/01/06/dhyana/

## Re: TRUE and FALSE are treated differently

Kimmo Laine wrote:

Unless next_page.php generates PHP, the script with this include will
only get HTML.

next_page.php

<?php
if (isset(\$_GET['foo'])) {
echo '<?php echo \$_GET[\'foo\']; ?>';
} else {
echo '<?php echo \'Not available\'; ?>';
}

--

## Re: TRUE and FALSE are treated differently

Kimmo Laine wrote:

Unless next_page.php generates PHP, the script with this include will
only get HTML.

next_page.php

<?php
if (isset(\$_GET['foo'])) {
echo '<?php echo \$_GET[\'foo\']; ?>';
} else {
echo '<?php echo \'Not available\'; ?>';
}

--

## Re: TRUE and FALSE are treated differently

Kimmo Laine wrote:

Unless next_page.php generates PHP, the script with this include will
only get HTML.

next_page.php

<?php
if (isset(\$_GET['foo'])) {
echo '<?php echo \$_GET[\'foo\']; ?>';
} else {
echo '<?php echo \'Not available\'; ?>';
}

--

## Re: TRUE and FALSE are treated differently

Kimmo Laine wrote:

Unless next_page.php generates PHP, the script with this include will
only get HTML.

next_page.php

<?php
if (isset(\$_GET['foo'])) {
echo '<?php echo \$_GET[\'foo\']; ?>';
} else {
echo '<?php echo \'Not available\'; ?>';
}

--

## Re: TRUE and FALSE are treated differently

Kimmo Laine wrote:

Unless next_page.php generates PHP, the script with this include will
only get HTML.

next_page.php

<?php
if (isset(\$_GET['foo'])) {
echo '<?php echo \$_GET[\'foo\']; ?>';
} else {
echo '<?php echo \'Not available\'; ?>';
}

--

## Re: TRUE and FALSE are treated differently

A Bit Narked wrote:

strlen(TRUE)==TRUE
strlen(FALSE)==FALSE

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.17.14-mm-desktop-9mdvsmp, up 11 days, 22:09.]

NetSol Cybersquatting
http://tobyinkster.co.uk/blog/2008/01/10/netsol-cybersquatting/

## Re: TRUE and FALSE are treated differently

.oO(A Bit Narked)

First: The internal values of the constants FALSE and TRUE are _not_ the
integers 0 and 1, but the two possible values of the type 'bool':

var_dump(constant('FALSE'));
bool(false)

var_dump(constant('TRUE'));
bool(true)

Second: If you print out a boolean, its value is automatically converted
to a string. How and why this is done is described in the manual:

| A boolean TRUE value is converted to the string "1", the FALSE value
| is represented as "" (empty string). This way you can convert back and
| forth between boolean and string values.

Micha

## Re: TRUE and FALSE are treated differently

On Sun, 06 Jan 2008 19:53:43 +0100,

You don't find that a daft convention?  It implies that true has
an integer value that can undergo autoconversion in a string
context but false has no value and cannot.   I can't find any
sense in that at all.

Puzzled wrote:

## Re: TRUE and FALSE are treated differently

wrote:

You assume some things which are just not right, to clarify:
1. An empty string is still as string, the fact that it's empty should not
matter.
2. True doesn't have an 'integer value', it's just that PHP is loosely
typed, with clearly defined casts when converting one into the other.
3. The types of variables PHP has are limited and clearly defined. Don't
assume they're actually something else, a boolean IS a boolean, not just
some mask of an integer, string, or something else.
--
Rik Wasmus

## Re: TRUE and FALSE are treated differently

Well, thanks to all for your responses.   The php developers get
2 marks out of 10 for this, in my book.  Arbitrary choices should
fit *with* people's expectations, not go against them.

## Re: TRUE and FALSE are treated differently

wrote:

*your* expectations, not mine.
--
Rik Wasmus