2

I am trying to print the following statement:

print false . "\n" . true . "\n";
echo false . (bool)false . "\n" . true . "\n";
print "0" . "\n" . true . "\n";

The result that I am getting is just "1 1 0 1". The expected result is:

0
1
0
1
0
1

I am using PHP 5.4.3 MSVC9 x64 Can someone please explain why and how I can make it print the correct way?

j0k
  • 22,600
  • 28
  • 79
  • 90
Randall Flagg
  • 4,834
  • 9
  • 33
  • 45

2 Answers2

10

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.

Community
  • 1
  • 1
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
2

This should do the trick. Use an array.

$boolarray = Array(false => 'false', true => 'true');
echo $boolarray[false],"  ", $boolarray[true];

Output : false true

Lion
  • 18,729
  • 22
  • 80
  • 110
  • @Lion Do you know how to print them on two lines? – Randall Flagg Jul 16 '12 at 21:42
  • `echo $boolarray[false],"
    ", $boolarray[true];`? Simply using a line-feed `
    `.
    – Lion Jul 16 '12 at 21:44
  • Not the answer I wanted but it will have to do. I would prefer a solution from within the language so I won't need to decalre / import this array every time. – Randall Flagg Jul 16 '12 at 22:25
  • See [this](http://stackoverflow.com/questions/4948663/php-get-bool-to-echo-false-when-false) question. What I mentioned in my answeris also there. I have just found it. Also, tried `echo (int)$bool_val;` as mentioned there in of the answers but unfortunately it also didn't work. – Lion Jul 16 '12 at 22:29
  • @Lion I tried that. I also tried Intval. Thanks anyway. – Randall Flagg Jul 16 '12 at 22:40
  • Tried much more. Apart from all of them, `echo (string)$var` should have done the job but it also didn't work as your requirement. – Lion Jul 17 '12 at 21:17
  • In brief, you need to check conditionally like `if(true) echo "true"; else echo "false";` or the same thing using the ternary/conditional operator. If you are able to find a concise solution, don't forget to share here. – Lion Jul 18 '12 at 15:39
  • @Lion `
    ` is not a line feed, but a line break.
    – Cole Tobin Jul 20 '12 at 03:19
  • @Cole Johnson:) Good call. I was just sleepy and forgot that "*`
    ` stands for **break***".
    – Lion Jul 20 '12 at 18:54