Logical Operators Manual Page Is Misleading
| From: | nomad311 at gmail dot com | Date: | Wed, 13 May 2009 11:53:19 +0000 |
| Subject: | Logical Operators Manual Page Is Misleading | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-4760@lists.php.net to get a copy of this message | ||
In Example #1 of the manual page for Logical Operators ( http://www.php.net/manual/en/language.operators.logical.php) the first section states that all operators are short-circuit yet in the subsequent sections you show the || and && operators completing both checks before returning a value. I have attached a test script that will show that these operators act differently when the result of the test is store and when it is used immediately. The output I receive from this script is as follows:
Test 1:
bool(false)
bool(true)
bool(true)
bool(false)
Test 2:
false
false
true
true
Test 3:
bool(false)
bool(false)
bool(true)
bool(true)
Below is also the script embedded test.php:
<?php
function compTest1(){
$a = true && false;
$b = true AND false;
$c = false || true;
$d = false OR true;
var_dump($a, $b, $c, $d);
}
function compTest2(){
if(true && false){
print 'true' . PHP_EOL;
}
else{
print 'false' . PHP_EOL;
}
if(true AND false){
print 'true' . PHP_EOL;
}
else{
print 'false' . PHP_EOL;
}
if(false || true){
print 'true' . PHP_EOL;
}
else{
print 'false' . PHP_EOL;
}
if(false OR true){
print 'true' . PHP_EOL;
}
else{
print 'false' . PHP_EOL;
}
}
function compTest3(){
var_dump(true && false);
var_dump(true AND false);
var_dump(false || true);
var_dump(false OR true);
}
print 'Test 1:' . PHP_EOL;
compTest1();
print PHP_EOL . 'Test 2:' . PHP_EOL;
compTest2();
print PHP_EOL . 'Test 3:' . PHP_EOL;
compTest3();
?>
-nomad311