Which PHP Logical Operator Is Better - ‘and’ or ‘&&’?

I was browsing the Sitepoint forum today and somebody raised this question - is it better to use ‘and’ or ‘&&’ as a logical operator in PHP?

In most cases, they evaluate the same way. For example, these two statements are equivalent.

if ($x && $y) { ... }
if ($x and $y) { ... }

So what’s the difference?

&& vs And - Is One Quicker?

Initially, I thought there may have been a speed factor. && and || are the traditional logical operators - the ones I used when I learned C++.

Therefore, it would be logical to assume that ‘and’ and ‘or’ are simple conveniences added to PHP. If these have to be re-routed through ‘&&’ and ‘||’ to actually evaluate, there would be a slight performance hit if ‘and’ was used instead of ‘&&’.

I did some benchmark testing to see if this turned out to be true, and there was no appreciable difference in execution time.

I designed a script (here’s the source code if you want to try it yourself) to test ‘&&’ verse ‘and’.

The basic timed task is to compare a set of numbers. In order to ensure that each time the script executed it did the same comparisons, I hard coded five sets of numbers.

if ((1 < 2) && (2 < 3) && (3 < 4) && (4 < 5) && (5 < 6)) {
  }

This was executed 100,000 times and timed. The time was recorded and the entire script was run over 100 times. At the end, an average was taken. The script was then modified to replace ‘&&’ with ‘and’ in the statement, and an average of 100 times was taken again.

The results showed no appreciable difference between using ‘&&’ and ‘and.’ Using ‘&&’ took an average of 0.152 seconds, while ‘and’ took an average of .155 seconds. At a little under 2% variation, I doubt that’s statistically significant.

I did notice that there were a few more outliers in the ‘and’ set - a couple times were slightly faster and a couple times were slightly longer than any of the ‘&&’ times.

Regardless, it appears that there’s no appreciable difference in execution time or efficiency between ‘and’ and ‘&&’.

Precedence in Evaluation

The only difference between the two operators, then, turns out to be their precedence.

As the documentation describes, “&&” has greater precedence than “and”. This is somewhat misleading. An example and some more accurate wording would help illustrate what this means.

This is the example in the documentation.

$g = true && false; // $g will be assigned to (true && false) which is false
$h = true and false; // $h will be assigned to true

The level of precedence between ‘&&’ and ‘and’ is rather irrelevant here. However, they both have different levels of precedence relative to the ‘=’ operator.

‘&&,’ like most operators, is evaluated before the ‘=’ operator. Therefore, the entire expression true && false is evaluated and stored in $g.

‘and’ on the other hand is evaluated after the ‘=’ operator. Therefore ‘true’ is stored in $h and then the expression $h and false can be evaluated.

Is There a Lesson Here?

Yes. Precedence of operators in PHP can be a bit strange.

Normal mathematical operations are normal enough. As expected, multiplication and division are evaluated before plus and minus. The PHP developers remembered the order of operations from middle school math.

However, other operators (like these logical operators) can have unpredictable consequences. Instead of relying on PHP’s hard-coded levels of precedence, you should use parentheses often to make sure that your logic is being evaluated the way you want it to be.

For example, you could fix the ‘&&’ - ‘and’ discrepancy with a set of parentheses.

$g = (true && false);
$h = (true and false);

Now we have explicitly told PHP to evaluate the ‘and’/'&&’ expression first and then assign a value to our variable. Likewise, you could use a set of parantheses around $h = true to revert to the other order of evaluation.

While you may understand these obscure rules, not everyone who comes along will. To prevent confusion in the future and needless headaches, tell PHP how you want expressions to be evaluated. Don’t let it decide for you.


Bookmark and Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

Tags: , , , ,

3 Comments to “Which PHP Logical Operator Is Better - ‘and’ or ‘&&’?”

  1. Atli said this on

    Very interesting. I always assumed ‘&&’ and ‘and’ (as well as ‘&’) were the same thing, just dressed up differently.

    It is also worth mentioning that the rules explained here appear to apply to the ‘||’ and ‘or’ operators as well.

  2. Walkere said this on

    Yes, the same will apply to || and ‘or’.

    However, it’s important to note that the ‘&’ operator is quite different from the ‘&&’ operator. The same goes for ‘|’ and ‘||’.

    ‘&’ and ‘|’ are bitwise operators. They compare the bits of two items and return either the intersection of those bits (&) or the union of those bits (|).

    For example, 7 can be written as 0000 0111 and 17 can be written as 0001 0001.

    7 & 17 = 0000 0001 (or 1)

    7 | 17 = 0001 0111 (or 23)

  3. Binary Code and Bitwise Operators (in PHP) | Web Cash said this on

    […] recognize them. If you don’t know what they are, it is easy to confuse them with the standard logical operators - || and […]

Leave a Reply