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

krsort() Places Integers Before Strings In Keys

krsort() used the PHP 7 way to compare values: then, strings would rank below integers, in particular below 0.

In PHP 8.2, strings are now ranking above integers, and are moved to the end of the sorted array. This is related to the change of rules in comparisons.

PHP code

<?php

$x = array('a' => 1, 
		   0 => 2, 
		   1 => 3, 
		   '0' => 4,
);
krsort($x);
print_r($x);

?>

Before

Array
(
    [1] => 3
    [a] => 1
    [0] => 4
)

After

Array
(
    [a] => 1
    [1] => 3
    [0] => 4
)

PHP version change

This behavior changed in 8.2.

© 2023-2026, Damien Seguy - Exakat