Your problem arises due to your error of the +
operator in strings in PHP. String concatenation operator .
since PHP is free to type, it doesn’t know if you want to concatenate or add strings.
I will break you:
print false + "\n" + true + "\n"; echo false+(bool)false + "\n" + true + "\n"; print "0" + "\n" + true + "\n";
First you can choose to stay with echo
or print
. Now, go ahead:
print false + "\n" + true + "\n";
PHP strings, when added (without contact) , evaluate to 0
. Therefore, this operator evaluates:
print 0 + 0 + 1 + 0;
which is equal to 1
. The rest follow this example. If you want your code to work, you must use the concatenation operator ( .
). If you want to write True
or False
, as .NET does, you can write a simple function:
function writeBool($var) { echo ($var) ? "True" : "False"; }
According to the fact that PHP is typing (which sucks if you ask me), everything that evaluates to True
will write "True". I still won’t use a function to do this, since function calls in PHP are expensive .
source share