On Mon, 16 Jun 2008 13:40:28 +0200, Dikkie Dik <di****@nospam.orgwrote:
>There's no functional difference. Both perform a logical OR operation,
just at different precedence levels.
There IS a functional difference: || is a "greedy" or (calculates both
inputs to determine the output,
Not true.
PHP 5.2.4:
<?php
function func1(){
echo __FUNCTION__;
return true;
}
function func2(){
echo __FUNCTION__;
return true;
}
if(func1() || func2()) echo 'foo';
?>
You expect:
func1func2foo
Actual output:
func1foo
(It does not matter wether || or 'or' is used.)
The only time func2() will be evaluated is when func1() retuns false.
whereas the keyword or is a "lazy" or that calculates one of the inputs
and only the other one if necessary. That is also the reason why the "or
die()" construct works. If it would always calculate both inputs, the
die function would always be called!
Nope, the 'or die()' after an assignment works because it has a lower
precedence as an assignment and an assignment can be used as a boolean
(if($result = some_func()))
Spelled out with ()'s:
$result = some_func() or die('Err');
===
($result = some_func()) or die('Err');
And:
$result = some_func() || die('Err');
===
$result = (some_func() || die('Err'));
Of course, both would die() on a failure of some_func(), however, when
using '||' and success of some_func() $result will always be a boolean,
hence the 'or' in this context.
Same applies to && and "and"
Euhm, what?
<?php
function func1(){
echo __FUNCTION__;
return false;
}
function func2(){
echo __FUNCTION__;
return false;
}
if(func1() and func2()) echo 'foo';//result: func1
if(func1() && func2()) echo 'foo';//result: func1
?>
No again.
That said, I usually use "and" and "or", just for legibility. I am fully
aware of the consequences, though.
Not that much aware :P
I'm afraid you must be mistaken with another language then PHP.
--
Rik Wasmus
....spamrun finished