website/errors/goto.labelUndefined.md
<?php declare(strict_types = 1);
function doFoo(): void
{
goto nonexistent;
}
The goto statement targets a label that does not exist in the same function or method scope. PHP requires the target label to be defined within the same scope as the goto statement. Jumping to a label defined outside the current function, closure, or class method is not allowed.
Common cases where this error occurs:
goto is inside a closure or anonymous classgoto is in the outer functionThis is a fatal error in PHP and cannot be ignored.
Define the target label in the same scope as the goto statement:
function doFoo(): void
{
- goto nonexistent;
+ goto end;
+ echo 'skipped';
+ end:
+ echo 'done';
}
If the goto is trying to jump across a closure or anonymous class boundary, restructure the code to avoid goto entirely:
function doFoo(): void
{
- outside:
$fn = function () {
- goto outside;
+ return;
};
}