16

I am trying to understand the below code but I am confused by the use of the && operator.
Please explain the purpose of the && operation in the following code

function getErrors($autoClean=TRUE) {
     $retVal = $this->getErrorMessages();
     $autoClean && $this->unsetErrorMessages();
     return $retVal;
}
Kevin Johnson
  • 913
  • 7
  • 24
Code Junkie
  • 187
  • 6

2 Answers2

30

Here, && acts as a short-circuit operator (see also the code example here).

  • If $autoClean evaluates to true, $this->unsetErrorMessages() will be executed.
  • If $autoClean evaluates to false, $this->unsetErrorMessages() will not be executed.

Using || instead of && would reverse this behavior.

The same behavior can obviously also be achieved by adding an additional if statement. In doing so:

  • a && b() can be rewritten as

    if (a) {
        b();
    }
    
  • a || b() can be rewritten as

    if (!a) {
        b();
    }
    

While the use of short-circuit operators can reduce the number of lines, it can also make code harder to read.

In terms of execution speed, there should be no noticeable difference.


Update

I have to retract my earlier statement about there being no noticeable difference in execution speed. Given the following two test scripts, i.e. a version with the short-circuit operator:

<?php
    $a = true;                                                                                                   

    function b() { global $a; $a = !$a; }

    for ($i = 0; $i < 10000000; $i++) {
        $a && b();
    }
?>

And a version with an if statement:

<?php
    $a = true;                                                                                                   

    function b() { global $a; $a = !$a; }

    for ($i = 0; $i < 10000000; $i++) {
        if($a) { b(); }
    }
?>

It turns out that the second version (with the if statement) is roughly 40% faster (~450ms vs. ~750ms) on my PHP 5.5.9 version running on Ubuntu 14.04 LTS (64 bit).

Results may vary for different PHP versions and operating systems, but at least on my machine I consistently notice a significant difference in execution speed.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • 1
    You can also use the literal word `or` and `and` e.g `if ((true or true) and true)` – zanderwar Aug 06 '15 at 05:47
  • Basically its working on the principle of short-circuiting.In case of OR(cnd1 || cnd2), if cnd1 == true, it will short circuit the operation and never goes for cnd2. Similarly, for AND it checks cnd2 only if cnd1 is true. – Pritam Aug 06 '15 at 05:51
  • 3
    Something it might be good to note: an equivalent if statement will probably be seen as cleaner code and more easily understood, even though it is 3x as many lines. Less lines aren't always better. – Kevin Johnson Aug 06 '15 at 06:25
  • 3
    Great answer. _amber_, this is also the explanation behind the `someStatement() or die();` pattern [you may see elsewhere in PHP](http://stackoverflow.com/q/432454/1426891). – Jeff Bowman Aug 06 '15 at 06:47
  • 1
    Just to note from an internals perspective, the first one needs an implicit boolean cast [on the second operand] and an AND operation, which isn't needed when just doing `if ($a) { … }`, which is making the first code slower. – bwoebi Aug 06 '15 at 12:59
16

Here, && acts as a short-circuit operator as told by @robby.

  • If you call getErrors(), $autoclean will be true, $this->unsetErrorMessages() will be executed.
  • If you call getErrors(false), $autoclean will be false, $this->unsetErrorMessages() will not be executed.

The line provides us a behaviour to prevent default execution of any code and also provides us facility to reduce lines of code as

$autoClean && $this->unsetErrorMessages();

will be equivalent to

if($autoClean){
   $this->unsetErrorMessages();
}
idmean
  • 14,540
  • 9
  • 54
  • 83
Ashwani Shukla
  • 609
  • 2
  • 11
  • 30
  • @robby that's what I told, See the answer, I mentioned your name too. I just explained it, so that amber doesn't confuse with `if` in future. – Ashwani Shukla Aug 06 '15 at 06:25
  • 9
    Something it might be good to note: The if statement will probably be seen as cleaner code and more easily understood, even though it is 3x as many lines. Less lines aren't always better. – Kevin Johnson Aug 06 '15 at 06:25
  • true @kevin , but some times optimization is needed like when you develop framework or something – Ashwani Shukla Aug 06 '15 at 06:26
  • 2
    @AshwaniShukla fair enough, I'm mostly used to compiled languages. At the very least a comment would be helpful for people not familiar with the language's idioms. – Kevin Johnson Aug 06 '15 at 06:35
  • 13
    @AshwaniShukla This case has *nothing* to do with optimization, it's obfuscation at best (and most likely common folly). – Cthulhu Aug 06 '15 at 07:39
  • 2
    @AshwaniShukla you can barely optimize PHP code anyways since PHP optimizes so much in the background by itself. – low_rents Aug 06 '15 at 08:55
  • Code optimization is a set of methods of code modification to improve code quality and efficiency. A program may be optimized so that it becomes of a smaller size, consumes less memory, executes more rapidly, or performs fewer input/output operations. And it has nothing to do with the language(PHP), what matters for optimization is the way you write your code. – Ashwani Shukla Aug 06 '15 at 09:11
  • 1
    I doubt most compilers will make a difference between the two statements, and I seriously doubt a difference, is any, will result in a perceivable improvement in speed. Either way, I think second-guessing the compiler (or interpreter, ftm) is not a good idea for trivial matters. – Tobia Tesan Aug 06 '15 at 09:54