473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1505
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
1
by: Mach | last post by:
What is the purpose of having margins? When positioning an element, should is use top, left, bottom, right, or top-margin, left-margin, bottom-margin, right-margin? It seems like there is no...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
5
by: rss | last post by:
Microsoft gives the following get available space function but no C# function and I have seen that a lot where system functions like that are only shown with examples for VB (Visual Basic) Can...
7
by: Janning Vygen | last post by:
Hi, i searched the docs and the archives and was really wondering that i have not found anything searching for "functional index primary key". i would like to have a table of members with...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
139
by: Joe Mayo | last post by:
I think I become more and more alone... Everybody tells me that C++ is better, because once a project becomes very large, I should be happy that it has been written in C++ and not C. I'm the only...
13
by: MartinRinehart | last post by:
Do these two give a different result? function positive() { return true; } var positive = function() { return true; }
34
by: vippstar | last post by:
On Nov 17, 1:43 pm, Ertugrul Söylemez <e...@ertes.dewrote: Well your blog entry starts with which is untrue and a bad way to start an article. Reading further, in your code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.