📖 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.
Daryle De Silva
VP of Technology
11+ years building and scaling web applications. Writing about what I learn in the trenches.
Related Articles
PHP's concatenation assignment operator (.=) will throw an 'Undefined variable' error if you use it on an uninitialized variable. Always…
1 min readHere's a subtle PHP gotcha that can corrupt your data: array_combine() creates order dependencies that break silently when you refactor….
1 min readYou're reading a method and hit something like this: $results = $categories->map(function ($category) use ($config) { return $category->items->filter(function ($item) use…
2 min read
Leave a Reply