For coders TYPO3 Tech Corner

Please stop using switch/case in PHP

Please stop using switch/case in PHP

Of course, you can also do without “default” or specify several values separated by commas:

Before:

switch ($variable) { case 'foo': $newVariable = anyFunctionFoo(); case 'bar': $newVariable = anyFunctionBar(); default: $newVariable = 'foobar'; }

After:

$newVariable = match ($variable) { 'foo' => anyFunctionFoo(), 'bar' => anyFunctionBar(), default => 'foobar', };

Of course, you can also do without “default” or specify several values separated by commas:

$newVariable = match ($variable) { 'foo', 'foobar' => anyFunctionFoo(), 'bar' => anyFunctionBar(), };

Back

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us