In what scenarios does using explicit references in PHP code lead to unexpected behavior, such as increasing the reference count?

Using explicit references in PHP code can lead to unexpected behavior, such as increasing the reference count, when passing variables by reference unnecessarily or when using reference assignment in a way that is not intended. To avoid this issue, it is important to only use references when necessary, such as when passing large objects or arrays to functions that need to modify them directly.

// Incorrect usage of explicit references leading to unexpected behavior
function modifyArray(&$array) {
    $array[] = 'new element';
}

$array = ['old element'];
modifyArray($array);
var_dump($array); // Output: array(2) { [0]=> string(10) "old element" [1]=> string(11) "new element" }

// Corrected code without using explicit references unnecessarily
function modifyArray($array) {
    $array[] = 'new element';
    return $array;
}

$array = ['old element'];
$array = modifyArray($array);
var_dump($array); // Output: array(2) { [0]=> string(10) "old element" [1]=> string(11) "new element" }