1.2.858. Not Or Tilde

There are two NOT operator in PHP : ! and ~. The first is a logical operator, and returns a boolean. The second is a bit-wise operator, and flips each bit.

Although they are distinct operations, there are situations where they provide the same results. In particular, when processing booleans.

Yet, ! and ~ are not the same. ~ has a higher priority, and will not yield to instanceof, while ! does.

The analyzed code has less than 10% of one of them : for consistency reasons, it is recommended to make them all the same.

<?php

// be consistent
if (!$condition) {
    doSomething();
}

if (~$condition) {
    doSomething();
}

?>

See also Bitwise Operators, Logical Operators and Operators Precedences.

1.2.858.1. Suggestions

  • Use the ! in logical expressions

  • Use the ~ in bitwise expressions, with integers for example

1.2.858.2. Specs

Short name

Structures/NotOrNot

Rulesets

All, Preferences

Exakat since

1.8.9

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

logical-operator, bitwise-operator

Available in

Entreprise Edition, Exakat Cloud