website/errors/nullCoalesce.variable.md
<?php declare(strict_types = 1);
function doFoo(): void
{
$value = $undefined ?? 'default';
}
The variable on the left side of the ?? (null coalesce) operator is never defined in the current scope. While PHP does not throw an error when using ?? with undefined variables (unlike a direct variable access), this typically indicates a bug -- the variable name may be misspelled, or the code that was supposed to define it is missing.
Define the variable before using it in the null coalesce expression:
function doFoo(): void
{
+ $defined = getData();
- $value = $undefined ?? 'default';
+ $value = $defined ?? 'default';
}
If the variable is intentionally optional (e.g., comes from an extract() call or a conditionally-executed block), restructure the code so the variable is always defined:
function doFoo(bool $condition): void
{
+ $result = null;
if ($condition) {
$result = compute();
}
$value = $result ?? 'default';
}