Menu

Error Identifier: goto.labelUndefined

← Back to goto.*

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

function doFoo(): void
{
	goto nonexistent;
}

Why is it reported? #

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:

  • The label name is misspelled or does not exist at all
  • The label is defined in an enclosing function, but the goto is inside a closure or anonymous class
  • The label is defined inside a closure or nested function, but the goto is in the outer function

This is a fatal error in PHP and cannot be ignored.

How to fix it #

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;
 	};
 }

Non-ignorable error #

This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.

Rules that report this error #

  • PHPStan\Rules\Keywords\GotoUndefinedLabelRule [1]
Theme
A
© 2026 PHPStan s.r.o.