compact() Throws Notice On Missing Variable
compact() collects variables in an array. When trying to compact() variable that don’t exist, compact() now emits warnings to signal the missing variables. They might be removed or created.
Invalid variable names, such as numeric values, are also reported.
PHP code
<?php
$name = 'Tobias';
$age = 28;
// class error, where the variable is confused with its content
var_dump(compact($name, $age));
// valid usage
// var_dump(compact("name", 'age'));
?>
Before
PHP Warning: compact(): Undefined variable $Tobias
Warning: compact(): Undefined variable $Tobias
array(0) {
}
After
PHP Warning: compact(): Undefined variable $Tobias
Warning: compact(): Undefined variable $Tobias
PHP Warning: compact(): Argument #2 must be string or array of strings, int given
Warning: compact(): Argument #2 must be string or array of strings, int given
array(0) {
}
PHP version change
This behavior changed in 8.1.