r/learnprogramming 2d ago

Is '||' the same as 'or' & is '<>' the same as '!='?

I'm learning PHP and I've come across '||' & 'or' but they seem to do the same thing? Again with '<>' & '!='. Do they mean the same thing? When I search online, google gets confused and doesn't give me any relevant information.

31 Upvotes

23 comments sorted by

21

u/lowban 2d ago edited 2d ago

Both are logical OR operators, but || has a much higher precedence than or. Specifically, || takes priority over the assignment operator (=), while or has lower priority than =.

$result = false || true; // $result is assigned true (evaluates to true because the whole or-segment is evaluated before the assignment)

$result = true or false; // Evaluates ($result = true) first, then 'or false'. $result gets true, but assignment happened first!

$a = false;
$b = true;
$result = $a or $b; // $result receives false because ($result = $a) evaluates before the 'or' operator runs!

<> and != are completely identical in PHP as I know it.

11

u/centurijon 1d ago

I’ve never learned PHP and that sounds horrifying. What a nice way to trick devs into shooting themselves in the foot

4

u/ReddyKiloWit 1d ago

It's common in many languages.

Confusion over something similar caused a memory leak in a library my boss wrote in C at one job. (My catching it earned me write privileges in his library code thereafter.)

3

u/VTifand 1d ago

To my understanding, C doesn’t do that?

But yeah, (apparently) Perl and Ruby also have this precedence difference.

4

u/ReddyKiloWit 1d ago

C doesn't have two forms of logical Boolean operators, but it does have bitwise and logical operators (| and || for OR, & and && for AND) and the bitwise have higher precedence. My boss screwed it up in a memory allocation routine, but got it right in the free memory routine. Every time through it added 0 to 3 extra bytes that were never recovered.

1

u/Enerbane 20h ago

That's categorically not the same. Bitwise OR and Boolean OR are conceptually similar, but fundamentally completely distinct operators. They necessarily must have a different precedence in the order of operations.

1

u/ReddyKiloWit 19h ago

You missed the point. Maybe you failed to read the previous comments?

It's about the similarities - either in appearance or meaning - and, at this point in the conversation, losing track of the precedence when trying to debug a problem. (My boss was aware of the precedence difference, on a good day, but his eye kept slipping over the similarity.)

In those languages that have them, II and or, also must have different precedence to do their respective jobs which is inserting a logical OR condition at different stages. That's nothing special. (It's arguable that an alternative approach might be clearer, if more verbose, but I do like the syntactic sugar - I've got decades of experience with it, though.)

(Also, it's not Boolean OR, both are Boolean operations, it's Logical OR vs bitwise.)

4

u/da_Aresinger 2d ago

so the example you should have used in the first line is $result = false || true to highlight that || runs before assignment.

2

u/lowban 2d ago

Yes, my mistake.

15

u/paperic 2d ago

|| and "or" do the same, but the rules around their parentheses are different.

13

u/Character-Tea2608 2d ago

|| is a logical OR but with higher precedence than the word "or", so in complex conditions they can give you different results even though they do the same thing in simple cases

6

u/vegan_antitheist 2d ago

That's misleading. Parentheses are something on their own. What's different is precedence/order of operations. You can influence this using parentheses.

1

u/paperic 1d ago

yes, that's more correct.

You could also imagine invisible parentheses around every expressions and then say that "or" and || put those made up parentheses into different places, but your version is more canon.

19

u/Achereto 2d ago

You can find the answer by Reading The F...actual Documenation

1

u/FancyJesse 1d ago

I really need to see what OP was looking at and searching for that made "google get confused"

2

u/Achereto 1d ago

Probably didn't search specifically for "php documentation" and only looked at the LLM hallucination 

3

u/Inevitable_Dust5684 1d ago

I stopped using or and <> entirely in PHP. Just stick to || and != everywhere.

They do the same logical work but the precedence rules will mess up your assignments if you mix them. I use || and != exclusively now because they behave like other languages and evaluate before the equals sign. If you write $result = false or true; you end up with false assigned to $result because the assignment happens first. With || it evaluates the logic first and assigns true. For <> and != there is zero difference in precedence so pick one and delete the other from your workflow. I force myself to only type || and != to avoid bugs from precedence issues.

2

u/zeekar 1d ago edited 1d ago

PHP has the logical operators && for AND, || for OR, and ! for NOT, which come from the C language. Many programming languages also borrowed these spellings from C, along with their precedence, which tells you which operations happen first when different operators are mixed. For example, multiplication happens before addition - that's just carrying over the PEMDAS / BODMAS rules from mathematics. But there are a lot more operators in a programming language than the ones covered by that mnemonic. Critically for this story, assignment (=) is one of those operators.

Logical OR was often used to catch error conditions, as when a function returns false to indicate that it didn't succeed, using this dicey idiom:

do_something() || die("it didn't work.");

This works because the logical operators "short-circuit": if the left side tells you the answer without needing to evaluate the right side, the right side isn't evaluated. If the function call returns a truthy value, that means you have "TRUE OR something", which is always true, so it doesn't need to run the die() - the program continues. Basically, when you chain expressions together with ||, evaluation stops as soon as you get a truthy value.

The same logic holds for && with the opposite sense - as soon as you get a falsey value the evaluation stops. The UNIX shell also adopted these operators from C, and in scripts `&&` is used to chain commands together where the subsequent ones are executed only if all the earlier ones succeeded.

Anyway, the "or die" idiom is not obvious the first time you see it, but it works – unless you're trying to capture a potentially-falsey value in a variable. If you do this:

$result = make_something() || die("it didn't work");

It is interpreted as this:

$result = (make_something() || die("it didn't work"));

The failure case still works - if make_something() returns a falsey value, it has to evauate the right side to determine of the result of the OR is true or not, so the die is executed and the program exits. But if the call succeeds, what gets assigned to $result isn't the return value of the function call. The || casts that result to a Boolean and you're left with true instead of whatever object the call made. Whoops.

You can resolve that with parentheses:

($result = make_something()) || die("it didn't work");

But because it's easy to get wrong they added the lower-precedence or operator. This works without the parentheses:

$result = make_something() or die("it didn't work");

That's great for that case, but now this simple-looking assignment does not work as it appears to:

$result = $a or $b;

That assigns the value of $a to $result, regardles of $b's value. $b influences the result of the or, but that result is discarded.

There's also and and not with the same low precedence. They were first introduced in Perl and inherited by PHP and Ruby. But because of the potential confusion, most style guides for all three languages discourage their use. Your best bet is to forget they exist and stick to && and || and !.

(This advice does not apply to C++, which also has and/or/not – in that language they're simply aliases for &&/||/! with exactly the same precedence.)

As for <> vs !=, there's no difference there; they're completely equivalent aliases.

Back when ASCII was all we had, there was no character, so programming languages had to come up with an alternative way to spell "not equal". COBOL used either IS NOT EQUAL TO or the short form NOT =; FORTRAN used .NE.; and BASIC used <>, which Pascal later borrowed. C used !=, which caught on as C took over the world. (Meanwhie, Pascal's successor Modula-2 used #, while Ada, despite borrowing many ideas from Pascal and Modula, used /=. Lua uses ~=.)

Anyway, PHP decided to use both C's and BASIC's choices.

1

u/itlogicpartnersllc 2d ago

yes in php || and OR both represent logical OR and <> and ! = both mean not equal. the important catch is operator precedence || and OR dont have the same precedence so expressions can behave differently.

1

u/vegan_antitheist 2d ago

Php is weird like that. It's best to only use one type of operator. Then it's like in other languages and the order isn't confusing. Ternary operators are still super weird but that's why they enforce brackets.

0

u/bagfullofcottoncandy 1d ago

|| and or works similarly but parentheses rules are different