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

Recursive Comparison Of Arrays

Recursive arrays should not be compared one another, as the engine might ends in an infinite loop. This happens when the array contains a reference to itself in one of its own elements. PHP did not caught such situation, and keep looping in the structure, even though it is infinite.

In PHP 8.4, it is now a catchable Error, and eventually stops.

PHP code

<?php

$array = [1,2,3, &$array];
$array2 = [1,2,3, &$array2];

var_dump($array == $array2);

?>

Before

PHP Fatal error:  Nesting level too deep - recursive dependency?

Fatal error: Nesting level too deep - recursive dependency?

After

PHP Fatal error:  Uncaught Error: Nesting level too deep - recursive dependency? 

Fatal error: Uncaught Error: Nesting level too deep - recursive dependency? 

PHP version change

This behavior changed in 8.4.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat