Last active
March 15, 2021 14:15
-
-
Save ameenross/85c3acf9c394fe478a723a9b99b2b039 to your computer and use it in GitHub Desktop.
Switch syntax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is for everyone who hates the indenting behavior of switch/case. | |
* | |
* If using PSR-2 and you're getting this error: | |
* PSR2.ControlStructures.SwitchDeclaration.WrongOpenercase | |
* @see https://github.com/squizlabs/PHP_CodeSniffer/issues/3016 | |
*/ | |
$var = 'foo'; | |
switch ($var) { | |
case 'foo': { | |
// Code. | |
break; | |
} | |
case 'bar': { | |
// More code. | |
break; | |
} | |
case 'baz': { | |
// Yet more code. | |
break; | |
} | |
default: { | |
// Not one of foo/bar/baz. | |
} | |
} | |
/** | |
* This is the old and stupid method that nobody uses anymore! /wishfulthinking | |
* | |
* Requires you to manually ident/unindent and doesn't look as nice either. | |
* Who wants that? | |
*/ | |
switch ($var) { | |
case 'foo': | |
// Code. | |
break; | |
case 'bar': | |
// More code. | |
break; | |
case 'baz': | |
// Yet more code. | |
break; | |
default: | |
// Not one of foo/bar/baz. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment