3

Given the statement:

if($value && array_key_exists($value, $array)) {

         $hello['world'] = $value;

        }

Would it be best to use the logical operator AND as opposed to &&?

Ryan McCullagh
  • 13,581
  • 8
  • 56
  • 101
  • 1
    they function identically, so it really doesn't matter. just pick one and stick with it would be my advice. – Dan Aug 19 '11 at 16:26
  • possible duplicate of [PHP - and / or keywords](http://stackoverflow.com/questions/4502092/php-and-or-keywords) – Gordon Nov 18 '11 at 11:03
  • *(related)* [What does that symbol mean in PHP](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon Nov 18 '11 at 11:04

4 Answers4

11

On their own, they do exactly the same. So a && b means the same than a and b. But, they are not the same, as && has a higher precedence than and. See the docs for further information.

This example here shows the difference:

// The result of the expression (false && true) is assigned to $e
// Acts like: ($e = (false && true))
$e = false && true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) and true)
$f = false and true;
theomega
  • 29,902
  • 21
  • 83
  • 125
3

The link you provide has a note:

The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See Operator Precedence.)

In your case, since it's the only operator, it is up to you, but they aren't exactly the same.

Dennis
  • 30,518
  • 9
  • 59
  • 75
3

They are the same in conditional statements, but be careful on conditional assignments:

// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;

// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;

and

// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;

From the Logical Operators manual you linked to.

Shef
  • 41,793
  • 15
  • 74
  • 88
1

only difference of spelling..its a good practice to use && instead of AND or and..

mysterious
  • 1,387
  • 4
  • 28
  • 54