3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', ]; function multisort($array) { array_multisort(array_map('strlen', $array), SORT_DESC, $array); printf( "Total strlen() calls: %d\n%s\n---\n", count($array), var_export($array, true) ); } function usersort($array) { usort( $array, function($a, $b) { echo "strlen() called twice\n"; return strlen($b) <=> strlen($a); } ); var_export($array); echo "\n---\n"; } multisort($array); usersort($array);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Total strlen() calls: 9 array ( 0 => 'eight', 1 => 'seven', 2 => 'three', 3 => 'five', 4 => 'four', 5 => 'nine', 6 => 'one', 7 => 'six', 8 => 'two', ) --- strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice array ( 0 => 'three', 1 => 'seven', 2 => 'eight', 3 => 'four', 4 => 'five', 5 => 'nine', 6 => 'one', 7 => 'two', 8 => 'six', ) ---
Output for 7.4.0 - 7.4.33
Total strlen() calls: 9 array ( 0 => 'eight', 1 => 'seven', 2 => 'three', 3 => 'five', 4 => 'four', 5 => 'nine', 6 => 'one', 7 => 'six', 8 => 'two', ) --- strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice strlen() called twice array ( 0 => 'seven', 1 => 'three', 2 => 'eight', 3 => 'four', 4 => 'five', 5 => 'nine', 6 => 'one', 7 => 'two', 8 => 'six', ) ---

preferences:
132.83 ms | 410 KiB | 5 Q