0

Why does PHP return 0 when a logical AND returns FALSE, but does not return the 0 when a conditional AND returns FALSE? Witness:

php > function a(){
php { echo "a";
php { return FALSE;
php { }

php > function b(){
php { echo "b";
php { return TRUE;
php { }

php > echo (a() && b())."\n";
a
php > 

php > echo (a() & b())."\n";
ab0
php >

Notice that the second echo statement ends with 0, yet the first does not. Why?

Machavity
  • 30,841
  • 27
  • 92
  • 100
dotancohen
  • 30,064
  • 36
  • 138
  • 197
  • it should return a T_PARSE error since aa() is undefined. – Tularis Feb 11 '14 at 10:35
  • You are not testing return values. You are testing casting to string. – Álvaro González Feb 11 '14 at 10:35
  • You have an explantarion in this link http://stackoverflow.com/questions/2376348/difference-between-and-in-php – Donnie Rock Feb 11 '14 at 10:37
  • Because PHP has no data types, you have to use ===. This compares the contents as well as the data type. – das_j Feb 11 '14 at 10:37
  • 1
    @das_j PHP very well *has* data types: http://php.net/types – deceze Feb 11 '14 at 10:38
  • 1
    @das_j Where is he doing any comparisons in his code? – Barmar Feb 11 '14 at 10:39
  • @deceze Yeah, I was wrong.... small sample to show what I mean: if a method returns integers, and `false` if an error happens, you should check the result with `===` – das_j Feb 11 '14 at 10:40
  • @dot You know that you could simplify this question to `echo false && true; echo false & true;`? – deceze Feb 11 '14 at 10:44
  • @DonnieRock: Thank you, I know about short circuiting. This is not a dupe of that question as I am not asking why the operation short circuits. I am asking about the trailing `0`. – dotancohen Feb 11 '14 at 10:57
  • @deceze: Yes, but I wanted to output the `a` and `b` to demonstrate the very short circuiting that Donnie mentioned. Maybe that fact confused the issue for him. – dotancohen Feb 11 '14 at 10:59

3 Answers3

5

&& returns a boolean. The boolean false when cast to a string is '', an empty string.
& is a bitwise and (not a "conditional and", whatever that is), which produces a number (or binary blob, depending on the arguments) as a result. In this case, the operation results in the number 0.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • This is not fully correct. The problem is actually caused by short-circuiting. Although this would then hide the boolean value as well – Tularis Feb 11 '14 at 10:40
  • This is perfectly correct. Short circuiting explains the `a`/`ab`, not the `0`. – deceze Feb 11 '14 at 10:41
  • Thank you! You point out three things that I did not realize: 1) Conditional and/or is not what I thought that it was, 2) I was performing a bitwise operation, and 3) The values that TRUE and FALSE return when cast to string. It was also enlightening to try to guess how other languages (Python for example) cast their booleans to strings as well. – dotancohen Feb 11 '14 at 18:25
2

In second case

php > echo (aa() & b())."\n";

is a bitwise AND. You need to use && for comparison

Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
0

Actually, neither of the answers provided are fully correct. The right answer is threefold.

The "problem" here is that && is a short-circuit operator. So it basically moves from the left-most argument to the right most one-by-one. For each one it checks if it returns true, if not, it stops and WILL NOT execute the next statement. PHP Documentation on logical operators.

Ergo: in the first case, you see only a (and not b) due to shortcircuiting. Then you also don't see the 0 because it's a boolean (not an int).

Then in the 2nd case you see an integer 0 because he's using a bitwise AND instead of a logical.

Tularis
  • 1,506
  • 1
  • 8
  • 17
  • So? How does this explain the presence or absence of `0`? – deceze Feb 11 '14 at 10:40
  • There is an absence of 0 as explained in the first answer. And in the second case, it's caused by a bitwise AND which returns an int as explained by the second answer. – Tularis Feb 11 '14 at 10:42