📖 1 minute read
PHP 8 is stricter about uninitialized variables. Using the concatenation assignment operator (.=) on a variable that hasn’t been declared will now throw an ErrorException in most modern framework environments.
// Throws ErrorException in PHP 8 if $content is undefined
foreach ($items as $item) {
$content .= $item->name;
}
// Correct
$content = '';
foreach ($items as $item) {
$content .= $item->name;
}
Always initialize your string variables before entering a loop where you append data. This ensures forward compatibility and makes your code’s intent clearer.
Leave a Reply