Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Comparison

Comparison operators check how two values relate to each other. They may be equal or different, and, depending on the type of compared data, they may be greater or lesser than the other one.

PHP comparisons exists with type-juggling, ==, <>, <=>, switch() and !=, and with type checks, match(), === and !==. They are called loose comparison, as PHP applies some cast before executing the comparison. Inequalities only exist with type-juggling.

On the other hand, PHP has strict comparisons, which include comparing the type before running any further checks. They are === and !==.

There are also functions dedicated to comparisons, with specific applications: strcmp(), strcasecmp(), strnatcasecmp(), strcoll(), similar_text(), levensthein(), bccomp(), version_compare(), hash_equals().

Some of the functions include a loose or a strict version.

<?php

    if ($a == $b) {
        print 'a and b are equal';
    }

?>

Documentation

See Also