470,835 Members | 1,938 Online

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
Jun 27 '08 #1
11 1416
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.
Jun 27 '08 #2
..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
Jun 27 '08 #3
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
Jun 27 '08 #4
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.
Jun 27 '08 #5
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.
js*******@attglobal.net
==================

Jun 27 '08 #6
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
Jun 27 '08 #7
..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
Jun 27 '08 #8
On Jun 16, 12:40 pm, Dikkie Dik <dik...@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, 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.
Jun 27 '08 #9
On Jun 16, 3:49 pm, Michael Fesser <neti...@gmx.dewrote:
>
foo() || die();
That so belongs on the front of a tee shirt. "Foo or DIE!"
Jun 27 '08 #10
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

Jun 27 '08 #11
>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.
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.