×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Or vs ||
2

Or vs ||

Or vs ||

(OP)
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

RE: Or vs ||

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

RE: Or vs ||

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 || diverts the open function's last parameter, ruining the logic :

CODE --> perl -de 42

  DB<1> open $F, '<no-such-file' or print "that's bad\n";
that's bad

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

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

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

Feherke.
feherke.ga

RE: Or vs ||

(OP)
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

RE: Or vs ||

Quote (feherke)


( 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. 2thumbsuppc2

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) or 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

RE: Or vs ||

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close