Skip to content

Instantly share code, notes, and snippets.

@flyingluscas
Last active April 17, 2017 17:31
Show Gist options
  • Save flyingluscas/cc48fd1c83228be268c6684a111e6531 to your computer and use it in GitHub Desktop.
Save flyingluscas/cc48fd1c83228be268c6684a111e6531 to your computer and use it in GitHub Desktop.
Array Replacement for Switch
$subject = 'flying';

switch ($subject) {
    case 'flying':
        $result = 'potato';
        break;

    case 'bacon':
        $result = 'is life';
        break;

    default:
        $result = 'default value here';
        break;
}
$subject = 'flying';

$data = [
    'flying' => 'potato',
    'bacon' => 'is life',
];

$result = isset($data[$subject]) ? $data[$subject] : 'default value here';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment