Your problem comes from your misconception of the +
operator on strings in PHP. The string concatenation operator is .
as PHP is loosely typed, it doesn't know if you want to concat or add the strings.
I'll break it down for you:
print false + "\n" + true + "\n";
echo false+(bool)false + "\n" + true + "\n";
print "0" + "\n" + true + "\n";
First off you might want to pick to stay with echo
or print
. Now, onward:
print false + "\n" + true + "\n";
PHP strings, when added (not contacted), evaluate to 0
. So this statement evaluates to:
print 0 + 0 + 1 + 0;
which is 1
. The other ones follow suit. If you want your code to work, you should use the concatenation operator (.
). If you want to write True
or False
like how .NET does, you could write a simple function:
function writeBool($var)
{
echo ($var) ? "True" : "False";
}
As per PHP's loose typing (which sucks if you ask me), anything that will evaluate to true
, will write "True". I would still discourage from using a function to do this as function calls in PHP are expensive.
`s. – JJJ Jul 16 '12 at 21:43