|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Given the functions:
function first() { echo "first\n"; return true; } function second() { echo "second\n"; return true; } Besides operator precedence, is there any functional difference (there doesn't appear to be any) between: if (first() || second()) { echo "done\n"; } and if (first() OR second()) { echo "done\n"; } Both return: first done Daniel Klein |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Daniel Klein wrote:
> Given the functions: > > function first() { > echo "first\n"; > return true; > } > function second() { > echo "second\n"; > return true; > } > > > Besides operator precedence, is there any functional difference (there > doesn't appear to be any) between: > > if (first() || second()) { > echo "done\n"; > } > > and > > if (first() OR second()) { > echo "done\n"; > } > > Both return: > > first > done > > > Daniel Klein I wasn't even aware that you could use OR or AND. From the manual: $e = false || true; // $e will be assigned to (false || true) which is true $f = false or true; // $f will be assigned to false I'll stick with || and && -- less confusing. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
..oO(Daniel Klein)
>Given the functions: > >function first() { > echo "first\n"; > return true; >} >function second() { > echo "second\n"; > return true; >} > > >Besides operator precedence, is there any functional difference (there >doesn't appear to be any) between: > >if (first() || second()) { > echo "done\n"; >} > >and > >if (first() OR second()) { > echo "done\n"; >} There's no functional difference. Both perform a logical OR operation, just at different precedence levels. Micha |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
sheldonlg wrote:
> Daniel Klein wrote: >> Given the functions: >> >> function first() { >> echo "first\n"; >> return true; >> } >> function second() { >> echo "second\n"; >> return true; >> } >> >> >> Besides operator precedence, is there any functional difference (there >> doesn't appear to be any) between: >> >> if (first() || second()) { >> echo "done\n"; >> } >> >> and >> >> if (first() OR second()) { >> echo "done\n"; >> } >> >> Both return: >> >> first >> done >> >> >> Daniel Klein > > I wasn't even aware that you could use OR or AND. From the manual: > > $e = false || true; // $e will be assigned to (false || true) which is true > $f = false or true; // $f will be assigned to false > > I'll stick with || and && -- less confusing. Yup, it's very important to now 'or' & 'and' have some of the lowest precedences, most importantly lower then '=', that's for instance why we can do: $result = mysql_query('some_query') or die('foobar!'); .... otherwise $result would always be a boolean ![]() Using 'or' is not a real problem, but often misunderstood so when '||' does the job, for the sake of future coders please use '||'. -- Rik Wasmus ....spamrun finished |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
> 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, 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! Same applies to && and "and" That said, I usually use "and" and "or", just for legibility. I am fully aware of the consequences, though. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Dikkie Dik wrote:
>> 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, 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! > > Same applies to && and "and" > > That said, I usually use "and" and "or", just for legibility. I am fully > aware of the consequences, though. > Incorrect. Neither || nor && will evaluate the second operand if the first one determines the result of the expression. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Mon, 16 Jun 2008 13:40:28 +0200, Dikkie Dik <dikkie@nospam.org> wrote:
>> 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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
..oO(Dikkie Dik)
>> 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, whereas the keyword or is a "lazy" or >that calculates one of the inputs and only the other one if necessary. Nope. PHP uses lazy evaluation all the time. >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! foo() || die(); also works. Micha |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
On Jun 16, 12:40 pm, Dikkie Dik <dik...@nospam.org> wrote:
> > 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, 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! > > Same applies to && and "and" > > That said, I usually use "and" and "or", just for legibility. I am fully > aware of the consequences, though. echo ((1) || (die ('0'))); echo (2); output: "1 2" You're thinking of short circuit evaluation. This means as soon as the zend engine has an answer to the question "Does this evaluate as true or false?" it stops evaluating any statements that have yet to run. In my example, the first statement, which effectively translat4es to if (1) is always true. Since the || statement translates to "is either the statement on the left or the statement on the right true?" and the statement on the left is always true as any non-zero value evaluates to true, the zend engine can answer "yes" as soon as the left hand expression is evaluated. This means itnever has to run the right hand expression to answer the question and so it doesn't, meaning the die() never gets executed. The behaviour is the same regardless of whether you use || or or. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
On Jun 16, 3:49 pm, Michael Fesser <neti...@gmx.de> wrote:
> > foo() || die(); That so belongs on the front of a tee shirt. "Foo or DIE!" |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
Daniel Klein schrieb:
> Given the functions: > > function first() { > echo "first\n"; > return true; > } > function second() { > echo "second\n"; > return true; > } > > > Besides operator precedence, is there any functional difference (there > doesn't appear to be any) between: > > if (first() || second()) { > echo "done\n"; > } > > and > > if (first() OR second()) { > echo "done\n"; > } > > Both return: > > first > done > > > Daniel Klein Both are *logical* Operators. The differenz is the priority of them. || ist higher than OR. (froam high to low: &&, ||, and, or) So if you have $a or $b || $c you must read it like that $a or ($b || $c) Interesting, when you mix AND, OR, &&, || So: $a or $b && $c is not the same as $a || $bc and $c |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
>> There IS a functional difference: || is a "greedy" or (calculates both
>> inputs to determine the output, 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! > > Incorrect. Neither || nor && will evaluate the second operand if the > first one determines the result of the expression. > Oops! I did not know that. I was sure I read it in the manual... Thanks for the correction. |
|
![]() |
| Outils de la discussion | |
|
|