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

array_intersect() Converts Values While Scanning Inputs

array_intersect() compares values as strings. Until PHP 8.6, non-string values were converted to strings lazily, during the pairwise sort-based comparisons used internally. In PHP 8.6, all input arrays (once at least two are known to be non-empty) are scanned upfront and their values converted to strings before any comparison happens. This changes the number and order of conversion warnings and __toString() calls – fewer calls overall, since each value is now converted once instead of possibly several times during comparisons – and can change results for a stateful __toString() implementation.

PHP code

<?php

class Stateful {
    private static $calls = 0;
    public function __toString(): string {
        self::$calls++;
        echo "toString call #".self::$calls."\n";
        return 'x';
    }
}

$a = [new Stateful()];
$b = ['x'];
$c = ['x'];

$result = array_intersect($a, $b, $c);
var_dump(count($result));

?>

Before

toString call #1
toString call #2
int(1)

After

toString call #1
int(1)

PHP version change

This behavior changed in 8.6.

See Also

Extension

© 2023-2026, Damien Seguy - Exakat