tags:

views:

82

answers:

1

case 1:

echo false  . 'test';

output:

test

case 2:

echo true  . 'test';

output:

1test

Why true cast to 1 but false nothing?

+3  A: 

Simple:

A value can be converted to a string using the (string) cast or the strval() function. String conversion is automatically done in the scope of an expression where a string is needed. This happens when using the echo() or print() functions, or when a variable is compared to a string.

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

http://php.net/manual/en/language.types.string.php#language.types.string.casting

deceze
If I had one complaint about PHP, it would be that anytime something goes wrong it simply disappears quietly. Why can't everything just blow up in a catastrophic explosion like C++?
Lotus Notes
@Byron activate error handling
OM The Eternity
@Byron I don't know what's supposed to be "wrong" here. It's simply weak typing and type juggling in action. Learn the conversion rules, they are part of the basics of PHP and pretty simple.
deceze
@OM The Eternity I always keep it on E_ALL | E_STRICT. What I meant was that it's more difficult to visually pinpoint where the string disappeared when echoing out variables for testing. @deceze, yes I'm thankful for empty strings evaluating to false so that we don't have to check $_POST values as being == '' and can just use the boolean.
Lotus Notes
@Deceze thanks for pointing me where I was wrong... Will keep that in my knowledge.. Thanks again...
OM The Eternity
For everyone confused about type juggling, start here: http://php.net/manual/en/language.types.type-juggling.php
deceze