# Functional difference between OR and ||

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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

## Re: Functional difference between OR and ||

Daniel Klein wrote:

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.

## Re: Functional difference between OR and ||

sheldonlg wrote:

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

## Re: Functional difference between OR and ||

.oO(Daniel Klein)

There's no functional difference. Both perform a logical OR operation,
just at different precedence levels.

Micha

## 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!

Same applies to && and "and"

That said, I usually use "and" and "or", just for legibility. I am fully
aware of the consequences, though.

## Re: Functional difference between OR and ||

Dikkie Dik wrote:

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

## Re: Functional difference between OR and ||

Oops! I did not know that. I was sure I read it in the manual...

Thanks for the correction.

## Re: Functional difference between OR and ||

:

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.

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');
=3D=3D=3D
(\$result = some_func()) or die('Err');

And:
\$result = some_func() || die('Err');
=3D=3D=3D
\$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.

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.

Not that much aware :P
I'm afraid you must be mistaken with another language then PHP.
-- =

Rik Wasmus
...spamrun finished

## Re: Functional difference between OR and ||

.oO(Dikkie Dik)

Nope. PHP uses lazy evaluation all the time.

foo() || die();

also works.

Micha

## Re: Functional difference between OR and ||

That so belongs on the front of a tee shirt.  "Foo or DIE!"

## Re: Functional difference between OR and ||

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.

## Re: Functional difference between OR and ||

Daniel Klein schrieb:

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