While some might not even be aware of it, PHP doesn't only have the != for weak-typed inequality but also <>. Since it is much less common, though, and also doesn't have the same visual harmony with the strong-typed inequality operator, !==, we propose to explicitly forbid its use in our coding standards, by adding the following second paragraph to the "Operators" section:

Checks for weak-typed inequality MUST use the != operator. The <> operator MUST NOT be used in PHP code.

Comments

dags created an issue. See original summary.

Perignon’s picture

+1. While I learned not equals as <> I prefer !=.

tizzo’s picture

Wow, I really thought that was already a standard. I double checked and don't see it. Weird.

+1

dawehner’s picture

Realistically +1, given https://stackoverflow.com/posts/723426/revisions but yeah != is not ANSI SQL

drunken monkey’s picture

Issue summary: View changes

Realistically +1, given https://stackoverflow.com/posts/723426/revisions but yeah != is not ANSI SQL

I thought they were talking about SQL operators, too, but those are already regulated (although Views violates it) and it seems there indeed is a <> PHP operator! Never heard of it before, though, nor seen it (I'm pretty sure).

So, yes, definitely +1! Having this in the code somewhere would be a huge WTF.

Updating IS with a concrete proposal.

dawehner’s picture

Status: Active » Needs review

Oh wow, I got totally confused. +10 for that.

donquixote’s picture

Generally +1.

Some notes that we might want to mention in the doc page:

  • While || and or have different operator precedence, != and <> apparently have the same operator precedence.
  • When given a choice, always pick the strong-typed comparison operators, === or !==.
    Maybe this does not really belong on the "coding standards" page, but it is, I think, an important thing to learn for Drupal developers.
drunken monkey’s picture

While || and or have different operator precedence, != and <> apparently have the same operator precedence.

I don't think we need to note that – why would we? If people shouldn't use the operator, its precedence is irrelevant. And if they are frantically searching for a reason to allow it after all, they'll soon discover there is none themselves, without us pointing it out.

When given a choice, always pick the strong-typed comparison operators, === or !==.

That seems like a completely different issue – and one far less clear-cut, I'd say.

tizzo’s picture

Issue tags: +final discussion
tstoeckler’s picture

Would be nice to add a note about using <> for SQL to this. I was confused as well...

chx’s picture

> If people shouldn't use the operator, its precedence is irrelevant

"You don't need the <> operator as its precedence is the same as != so please use !="

joelpittet’s picture

+1

dawehner’s picture

Title: Prefer != to <> for NOT EQUALS » Prefer != to <> for NOT EQUALS in PHP
dawehner’s picture

So this issue is now in phase 4, right?
I guess the next step is logically step 5, so let's see when something happens next.

jthorson’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -final discussion +Approved Coding Standard, +needs documentation updates

Discussed at the August 9th coding standards committee meeting, and ratified.

As there were no immediately identifiable occurrences of this in core, this does not appear to affect core (i.e. is presumed as an existing defacto core policy), and is thus promoted to step 8 of the coding standards process.

tizzo’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -needs documentation updates

I updated the documentation, marking fixed!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.