Skip to content

Instantly share code, notes, and snippets.

@janogarcia
Created December 12, 2012 15:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save janogarcia/4268731 to your computer and use it in GitHub Desktop.
Save janogarcia/4268731 to your computer and use it in GitHub Desktop.
PHP Logical operators: The difference between OR vs ||, AND vs && explained. Key concepts: Logical operators precendence. Logical operators short-circuit evalutation.
<?php
// PHP Logical operators: The difference between OR vs ||, AND vs &&
// Key concept #1: "||" and "&&" have greater precedence than "=", "OR", "AND"
// http://php.net/manual/en/language.operators.precedence.php
// Key concept #2: PHP logical operators are short-circuit
// http://en.wikipedia.org/wiki/Short-circuit_evaluation
// http://php.net/manual/en/language.operators.logical.php
// DEMO: Test this snippet at http://codepad.viper-7.com/7ROnzP
$a = 'a';
$b = 'b';
$zero = 0;
$value = NULL;
// The logical "OR" operator has lower precendence than the assignment "=" operator
// The first assignment "$value = $a" evaluates to TRUE, thus the assignment on the right "$value = $b" will not be reached
var_dump($value = $a OR $value = $b); // TRUE bool('a')
// "$value" is assigned the value of the first assignment, "$value = $a"
var_dump($value); // 'a'
// The first assignment "$value = $zero" evaluates to FALSE, thus the assignment on the right "$value = $b" will be reached
var_dump($value = $zero OR $value = $b); // TRUE bool(0 OR 'b')
// "$value" is assigned the value of the last assignment, "$value = $b"
var_dump($value); // 'b'
// The logical "||" operator has greater precendence than the assignment "=" operator
// The first assignment "$value = $a" evaluates to TRUE, thus the assignment on the right "$value = $b" will not be reached
var_dump($value = $a || $value = $b); // TRUE bool('a')
// "$value" is assigned the value of "$a || $value = $b"
var_dump($value); // TRUE bool('a' || 'b')
// The first assignment "$value = $zero" evaluates to FALSE, thus the assignment on the right "$value = $b" will be reached
var_dump($value = $zero || $value = $b); // TRUE bool(0 || 'b')
// "$value" is assigned the value of "$zero || $value = $b"
var_dump($value); // TRUE bool(0 || 'b')
// The logical "AND" operator has lower precendence than the assignment "=" operator
// The first assignment "$value = $a" evaluates to TRUE, thus the expression on the right "$value = $b" will be reached
var_dump($value = $a AND $value = $b); // TRUE bool('a' AND 'b')
// "$value" is assigned the value of the last assignment, "$value = $b"
var_dump($value); // 'b'
// The first assignment "$value = $zero" evaluates to FALSE, thus the expression on the right "$value = $b" will not be reached
var_dump($value = $zero AND $value = $b); // FALSE bool(0)
// "$value" is assigned the value of the first assignment, "$value = $zero"
var_dump($value); // 0
// The logical "&&" operator has greater precendence than the assignment "=" operator
// The first assignment "$value = $a" evaluates to TRUE, thus the assignment on the right "$value = $b" will be reached
var_dump($value = $a && $value = $b); // TRUE bool('a' && 'b')
// "$value" is assigned the value of "$a && $value = $b", that is, TRUE
var_dump($value); // TRUE bool('a' && 'b')
// The first assignment "$value = $zero" evaluates to FALSE, thus the assignment on the right "$value = $b" will not be reached
var_dump($value = $zero && $value = $b); // FALSE bool(0)
// "$value" is assigned the value of "$zero && $value = $b", that is, FALSE
var_dump($value); // FALSE bool(0 && 'b')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment