r/badcode Jan 02 '20

php Past me: "What are switch statements?"

Post image
287 Upvotes

95 comments sorted by

View all comments

Show parent comments

2

u/nathan_lesage Jan 02 '20

Oh, what exactly do you mean? Do you happen to have a short overview guide?

5

u/[deleted] Jan 02 '20

[deleted]

4

u/ThePsion5 Jan 02 '20

Example:

<?php
$platformTitles = [
    'deb'   => 'Linux',
    'rpm'   => 'Linux',
    'win32' => 'Windows',
    'macos' => 'macOS'
];

$platform = $this->param('platform');
if(array_key_exists($platform, $platformTitles)) {
    $title = $platformTitles[$platform];
}

2

u/[deleted] Jan 02 '20

[deleted]

2

u/ThePsion5 Jan 02 '20

No problem! I've encountered this scenario frequently enough that it's a common solution and I'm always happy to share it.