Skip to content

Instantly share code, notes, and snippets.

@ameenross
Last active March 15, 2021 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ameenross/85c3acf9c394fe478a723a9b99b2b039 to your computer and use it in GitHub Desktop.
Save ameenross/85c3acf9c394fe478a723a9b99b2b039 to your computer and use it in GitHub Desktop.
Switch syntax
<?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