“usort(): Returning bool from comparison function is deprecated”: what it means and the fix
Short answer
It’s a deprecation notice from PHP 8.0 and later: a plugin or theme sorts a list with usort() (or uasort(), uksort()), and its comparison function answers true or false instead of a number. PHP 8 still sorts the list correctly, and adds the notice. The function should return a number below, equal to or above zero, which the “spaceship” operator does: $a <=> $b instead of $a > $b. Update the plugin or theme, or send the message to its developer.
The message, exactly
A price-list plugin sorts prices with a comparison that answers $a > $b. PHP 7.4 sorts silently; PHP 8.0 sorts the same way and adds the notice:
wp-content/plugins/price-list/includes/sort.php
<?php
$prices = [30, 10, 20];
usort($prices, function ($a, $b) {
return $a > $b;
});
echo implode(', ', $prices), "\n";
10, 20, 30Deprecated: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero in /var/www/html/wp-content/plugins/price-list/includes/sort.php on line 5
10, 20, 30php.net, in the PHP 8.0 deprecations: “Sort comparison functions that return true or false will now throw a deprecation warning, and should be replaced with an implementation that returns an integer less than, equal to, or greater than zero.” (php.net)
What it means
To sort, PHP compares two items at a time and asks the plugin’s function: is the first one smaller, equal, or bigger? The answer must be a number: below zero, zero, or above zero. $a > $b only says yes or no, which mixes up “smaller” and “equal”. PHP 8 copes with it, but marks it as deprecated.
Who fixes it
- Find the plugin or theme: the folder after
wp-content/plugins/orwp-content/themes/in the message. - Update it (Dashboard → Updates).
- Already on the latest version? Send one full line of the message to its developer: the fix is one operator.
- Meanwhile, keep notices off your pages: the WordPress debug log.
Found before you switch
The comparison is in the code, so a check that reads code finds it while your site still runs PHP 7.4:
It’s one of the PHP 8.0 changes covered in moving from PHP 7.4 to PHP 8; most of the others there stop the page, this one doesn’t.
Key takeaways
- A deprecation notice from PHP 8.0 and later: the list is still sorted, PHP adds a log line.
- The comparison function returns
$a > $b(true or false) instead of a number. - The fix is one operator:
$a <=> $binstead of$a > $b. - It applies to
usort(),uasort()anduksort()alike; the message names the function. - A check that reads code finds these before you upgrade.
Frequently asked questions
Is the order on my site wrong?
No. In our test the list came out sorted the same on PHP 7.4 and PHP 8.0; PHP 8 handles the true-or-false answer and only adds the notice.
Why does PHP want a number?
A sort needs three answers: smaller, equal or bigger. True or false only gives two, so PHP has to guess the third. A number below, at or above zero gives all three.
Is it urgent?
No. It’s a notice, not an error. Pass it to the developer so it’s fixed before a future PHP version stops accepting it.
Check your own site before you upgrade
CompatNav is a free WordPress plugin. It reads the code of your plugins and themes on your own server and tells you, in plain words, what will break and what will only show notices on the PHP version you choose. It never changes your code. It can’t see problems that only appear while code runs with real data, so a quick check of your site after the upgrade still matters.
Sources
About the code examples: each output is the real output of the code shown, run with the official PHP builds, without a php.ini, with all errors reported and displayed. Only the file path was replaced by a neutral server path.