Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.