Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Or vs || 2

Status
Not open for further replies.

kjv1611

New member
Jul 9, 2003
10,758
US
I'm going through a tutorial to get a better feel of php since Drupal is based off of it and because I have been interested for a long time anyway.

In the tutorial on a quiz question, I got a wrong answer for using "or" instead of "||".

Can anyone explain where "||" would work but "or" would not?

My initial thought is they boyh work the same but the quiz was just set for one specific answer.

Sorry for the quotation marks instead of just using tgml to make bold instead. I'm typing from my phone.

Thanks for any help

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
The difference is the precedence they take when more than one operation is being made.

or has less precedence than || and less precedence than several other operations

example:

Code:
$a=true;
$b=false;

$c = $b or $a;

$d = $b || $a;


//$c will be false, while $d will be true;

The assignment operation takes precedence over the or so its evaluated first. and $b gets assigned to $c. however || takes precedence over assignment, and as such the comparison is evaluated before the assignment is made, so $d is set to true since the comparison evaluates to true.

With that said, by default, in PHP || is usually the logical operator to use in comparisons unless you need the specific lower precedence of or for some reason.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi

( Phil's explanation is great, I just reproduce the explanation that worked for me. When I had this question ( ~15 years ago [ponder] ) Phil was not around... )

These have more sense in Perl, where 1) chaining function calls with logical operators is more frequent habit than in PHP; 2) parenthesis are optional in some cases for function calls.

A Perl example, showing how the high priority [tt]||[/tt] diverts the [tt]open[/tt] function's last parameter, ruining the logic :
Code:
  [blue]DB<1>[/blue] open $F, '<no-such-file' or print "that's bad\n";
that's bad

  [blue]DB<2>[/blue] open $F, '<no-such-file' || print "that's bad\n";

  [blue]DB<3>[/blue] open($F, '<no-such-file') || print "that's bad\n";       
that's bad
So in Perl the simple explanation is : [tt]||[/tt] between operands of an expression, [tt]or[/tt] between function calls.

As PHP's syntax is less flexible in this regards, the [tt]||[/tt] vs. [tt]or[/tt] dilemma is less significant. Just use with [tt]||[/tt]. ( Is more readable than using [tt]or[/tt] and remedying its low priority by sticking parenthesis everywhere. )


Feherke.
feherke.ga
 
Thanks a ton to both of you. I'm still going to take some time to swallow all of that. I know it's simple, but I guess from what I'm used to, or means or and that's it. Well, there are conditional statements and such, but generally speaking, or is just or. So either I'm just hard headed, or brain not woken up good yet, or it'll just simply take some time to take it all in.

At least with your responses, I do think I understand it a little bit now. Hopefully I'll figure most the rest out on my own, but I imagine over the coming weeks/months, maybe, I might pop up with another question.



"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
feherke said:
( Phil's explanation is great, I just reproduce the explanation that worked for me. When I had this question ( ~15 years ago ponder ) Phil was not around... )
LOL!. Sorry, I wasn't around. Was just learning it myself at around that time. [2thumbsup][pc2]

In any case, Feherke makes an excellent point, or is also used between functions calls like when wanting to run a second function if one function returns something unexpected.

i.e

Code:
$res = mysql_query($qry) [b][COLOR=#A40000]or[/color][/b] die(mysql_error());
Here you want the assignment to happen first, and only if the assignment cannot be completed for any reason, i.e mysql_query errors and returns a false then you want the die statement to execute and show you the error.

If you were to use ||, then it would run the comparison first every time, and simply assign the result of it to $res, instead of the query result handle. So you'd either be getting true as the value for $res, or killing your script, but never the query result handle to be used later to fetch the results.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thanks! That helps a ton, knowing or can be used between functions as well as comparing values. So I suppose if your functions simply return a value, then you could still use the || operator to compare accurately, but if the functions are truly doing something, you'd want to use or?

And why in the world did someone in building php come up with 2 pipes as an operator. Pipes are often used as delimiters mainly because they are rarely used within text for various items. So say you had a SQL table that had PHP code in it.. so I imagine a website could work this way.. if the || operator is ever used (and sounds like that's a high probability), then you should dare not export to a pipe delimited text file.


"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top