Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this syntax valid?

Tags:

php

I've answered a question today that had a simple solution, but I've yet to understand why it was something for which the PHP interpreter didn't throw a syntax error. So my question is simple:

Why are the statements below considered to have valid syntax?

SomeIdentifier:;
AnythingGoesApparently:;
ThisCanGoOnAndOn:;

Even more so since those identifiers are not defined anywhere.


To my knowledge the colon : is only used as part of two operators: The Scope Resolution Operator, The Ternary Operator, but not on its own. It's also used in the Alternative syntax for control structures. But none of these would qualify in this case, so this is bugging me like crazy.

like image 270
Bogdan Avatar asked Feb 05 '16 21:02

Bogdan


People also ask

What is a valid syntax?

What Does Syntax Validation Mean? Syntax validation is the process of checking whether the syntax of a program is free of programming or stylistic editors. There are a number of tools to check syntax for almost every programming language. Some are run locally on the computer and others are available online.


1 Answers

I believe these would be interpreted as goto labels.

For example:

$x = 0;
goto ThisIsBad;
$x++;

ThisIsBad:
$x += 2;

echo $x;

Output: 2

Reference: http://php.net/manual/en/control-structures.goto.php

like image 154
kunruh Avatar answered Sep 21 '22 11:09

kunruh