PHP NOT operator

Summary: in this tutorial, you will learn how to use the PHP NOT operator (!) to build complex logical expressions.

Introduction to the PHP NOT operator

Unlike the logical AND and OR operators that accept two operands, the logical NOT operator accepts only one operand and negates the operand.

In other words, the logical NOT operator returns true if the operand is false and returns false if the operand is true.

PHP uses the both not keyword and (!) to represent the logical NOT operator.

not expression

Or

! expression

The following table illustrates the result of the logical NOT operator:

Expressionnot Expression
truefalse
falsetrue

The logical NOT operator is also known as the logical negation operator.

PHP NOT operator examples

The following example illustrates how to use the logical not operator (!):

<?php

$priority = 5;
var_dump( ! $priority < 5 );Code language: HTML, XML (xml)

Output:

bool(true)Code language: JavaScript (javascript)

In this example, PHP evaluate the expression ! $priority < 5 in the following order:

  • First, $priority < 5 evaluates to false.
  • Second, ! false evaluates to true.

Summary

  • PHP NOT operator (not, !) accepts an operand and negate the result of the operand.
Did you find this tutorial useful?