Curriculum
This tutorial will teach you how to use the PHP NOT operator (!) to construct complex logical expressions.
In contrast to the logical AND and OR operators, which accept two operands, the logical NOT operator accepts only one operand and negates it.
In other words, the logical NOT operator returns true when the operand is false and false when it is true.
To represent the logical NOT operator, PHP employs both the not keyword and (!)
not expression
OR
! expression
The following table shows the outcome of the logical NOT operator:
Expression | not Expression |
---|---|
true |
false |
false |
true |
The logical NOT operator, also known as the logical negation operator, is a type of logic operator.
The example below shows how to use the logical not operator (!):
<?php $priority = 5; var_dump( ! $priority < 5 );
Output
bool(true)
PHP evaluates the expression in this example ! $priority 5 is assigned in the following order:
To begin, $priority 5 evaluates to false.
Second, the expression! false evaluates to true.