array_filter() Validates Its Mode Argument

array_filter() accepts a third argument that selects which values are passed to the callback: ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH. Until PHP 8.6, any other value was silently treated as 0 (ARRAY_FILTER_USE_VALUE). In PHP 8.6, an invalid mode throws a ValueError.

PHP code

<?php

var_dump(array_filter([1, 0, 2, null], fn($v) => true, 99));

?>

Before

array(4) {
  [0]=>
  int(1)
  [1]=>
  int(0)
  [2]=>
  int(2)
  [3]=>
  NULL
}

After

array_filter(): Argument #3 ($mode) must be one of ARRAY_FILTER_USE_VALUE, ARRAY_FILTER_USE_KEY, or ARRAY_FILTER_USE_BOTH

PHP version change

This behavior changed in 8.6

See Also

Error Messages