Semi-colon after the case valid in the PHP switch statement?

I am debugging some code for the client and found the following syntax:

switch ($i) { case 0; echo "i equals 0"; break; case 1; echo "i equals 1"; break; case 2; echo "i equals 2"; break; } 

Case statements end in semicolonies, not colons. Turns out this compiles, but is this legal? I have never seen this syntax before.

+6
source share
3 answers

From the documentation :

You can use a semicolon instead of a colon after the case, for example:

 switch($beer) { case 'tuborg'; case 'carlsberg'; case 'heineken'; echo 'Good choice'; break; default; echo 'Please make a new selection...'; break; } 
+12
source

As you can check here, it works: http://codepad.org/hOLQP98D I think it works because it crashes through

-1
source

Yup, while $i has a numeric value

-2
source

Source: https://habr.com/ru/post/908363/


All Articles