Logo

PHP: Negation

Alongside the logical operators AND and OR, the "negation" operation is also frequently used. It changes a logical value to its opposite. In PHP, negation corresponds to the unary operator !:

<?php

!true; // false
!false; // true

For example, if there's a function that checks whether a number is even, then with negation you can perform a check for oddness:

<?php

function isEven(int $number): bool
{
    return ($number % 2) === 0;
}

var_dump(isEven(10)); // => bool(true)
var_dump(!isEven(10)); // => bool(false)

In the example above, we added ! to the left of the function call and got the opposite action.

Negation lets you express the intended rules in code without writing new functions. If you write !!isEven(10), the code will work even in such a case:

<?php

var_dump((bool) isEven(10)); // => bool(true)

In logic, double negation is equivalent to no negation:

<?php

(bool) true; // true
(bool) false; // false

var_dump((bool) isEven(10)); // => bool(true)
var_dump((bool) isEven(11)); // => bool(false)

! can be combined with && and ||. Among the logical operators it has the highest precedence, so it is applied first:

<?php

!true || true; // (!true) || true   => false || true  => true
!true && false; // (!true) && false  => false && false => false

Parentheses change the order of evaluation:

<?php

!(true || true); // !true  => false
!(true && false); // !false => true

A practical example — a function checks whether a driver can get behind the wheel: a license and sobriety are required:

<?php

function canDrive(bool $hasLicense, bool $isDrunk): bool
{
    return $hasLicense && !$isDrunk;
}

var_dump(canDrive(true, false)); // => bool(true)  (has a license, sober)
var_dump(canDrive(true, true)); // => bool(false) (has a license, but drunk)
var_dump(canDrive(false, false)); // => bool(false) (no license)

Now you know what the operators AND, OR, and ! mean. With their help you'll be able to set compound conditions made of two or more logical expressions.

De Morgan's Laws

When working with complex logical expressions, you sometimes need to invert them or rewrite them in an equivalent form that's easier to read. For this there are De Morgan's laws — two rules that describe how negation distributes over a compound expression:

!(A && B)  is equivalent to  !A || !B
!(A || B)  is equivalent to  !A && !B

The first law: the negation of a conjunction equals the disjunction of the negations. Let's check:

<?php

!(true && false); // !false => true
!true || !false; // false || true => true

The second law: the negation of a disjunction equals the conjunction of the negations:

<?php

!(true || false); // !true => false
!true && !false; // false && true => false

In practice, De Morgan's laws help simplify conditions. For example, instead of !($isAdmin || $isModerator) you can write !$isAdmin && !$isModerator — it reads as "not an administrator and not a moderator".

Instructions

In a word game, players check whether a word is a palindrome. Implement two functions isPalindrome() and isNotPalindrome() that take a string as input

  1. The isPalindrome() function determines whether a word is a palindrome or not. A palindrome is a word that reads the same in both directions.

    <?php
    
    isPalindrome('wow'); // true
    isPalindrome('mom'); // true
    isPalindrome('hexlet'); // false
    
    // Words can be passed to the function in any case
    // Therefore, you must first convert the word to lowercase with strtolower()
    isPalindrome('Wow'); // true

    To reverse a word, use the strrev() function.

  2. The isNotPalindrome() function checks that a word is NOT a palindrome:

    <?php
    
    isNotPalindrome('wow'); // false
    isNotPalindrome('mom'); // false
    isNotPalindrome('hexlet'); // true

    To do this, call the isPalindrome() function inside isNotPalindrome() and apply negation.

Tips

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!