PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Functional difference between OR and ||
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Functional difference between OR and ||

Réponse
 
LinkBack Outils de la discussion
Vieux 13/06/2008, 15h23   #1
Daniel Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Functional difference between OR and ||

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
  Réponse avec citation
Vieux 13/06/2008, 16h23   #2
sheldonlg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

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.
  Réponse avec citation
Vieux 13/06/2008, 16h51   #3
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

..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
  Réponse avec citation
Vieux 13/06/2008, 16h51   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

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
  Réponse avec citation
Vieux 16/06/2008, 12h40   #5
Dikkie Dik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

> 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.
  Réponse avec citation
Vieux 16/06/2008, 13h24   #6
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

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
==================

  Réponse avec citation
Vieux 16/06/2008, 13h29   #7
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

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
  Réponse avec citation
Vieux 16/06/2008, 15h49   #8
Michael Fesser
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

..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
  Réponse avec citation
Vieux 16/06/2008, 16h21   #9
Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

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.
  Réponse avec citation
Vieux 16/06/2008, 16h24   #10
Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

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!"
  Réponse avec citation
Vieux 16/06/2008, 16h47   #11
Olaf Schinkel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

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



  Réponse avec citation
Vieux 17/06/2008, 09h54   #12
Dikkie Dik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Functional difference between OR and ||

>> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h17.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22197 seconds with 20 queries