Page 1 of 1

True/False Question

Posted: Mon May 22, 2006 6:58 pm
by BigAbe
Ok... We're having a small office debate about this, so I thought I would throw this at you guys.

Code: Select all

$var = true;

echo $var;
Gives us 1

BUT

Code: Select all

$var = false;

echo $var;
Gives us "" (no printout)

Why is this and can this be changed?

-- Abe --

Posted: Mon May 22, 2006 7:07 pm
by Christopher
I think you are seeing a behavior of echo rather than the internal representation of the data. It's probably just how Rasmus first implemented it (maybe because of how Perl interpreted the vars) and it has stayed that way. To really check the vars try:

Code: Select all

$var=true;
var_dump($var);
$var=false;
var_dump($var);

Posted: Mon May 22, 2006 7:09 pm
by Ambush Commander
You could also...

Code: Select all

echo (int) $var;
Which would print 0 for false.

Posted: Mon May 22, 2006 7:10 pm
by BigAbe
I'm just confused as to why echoing a false variable doesn't result in a "0" versus an empty printout.

-- Abe --

Posted: Mon May 22, 2006 7:13 pm
by Ambush Commander
You're casting a boolean object to a string. The behavior is documented here: http://us2.php.net/manual/en/language.t ... ng.casting claiming that setting it empty will make it possible to cast back to boolean (although a string = '0' also casts to zero).

It's one of life's little mysteries. I'd believe arborint.

Posted: Mon May 22, 2006 9:17 pm
by RobertGonzalez
I think that passing checkboxes follows along the same lines. When a checkbox array var is passed that is checked, it passes as 'On', but when it is not checked it passes as ''. I always wondered this, but never enough to bring it up. Thanks for starting the thread.

Posted: Mon May 22, 2006 9:18 pm
by Ambush Commander
Actually, for checkboxes, it's just not set at all. ;-)

Posted: Mon May 22, 2006 9:19 pm
by RobertGonzalez
Sorry, I wrote that wrong. You are right AC. They are not set at all.