|
|
|
|
|
by gremlinsinc
1334 days ago
|
|
match is basically a cleaner, more succinct switch, I hardly ever used switches before, though I use match a LOT. switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}
verses: $output = match($i) {
0 => 'i equals 0',
1 => 'i equals 1',
2 => 'i equals 2',
default => 'i is unknown'
}
echo $output;
the result is basically the same, but match is more of an enclosed switch that just returns instead of does side effects, then you can do what you like w/ that.Ymmv, but match is a great code saver, and is very nice for enums and methods on enums. Before match i'd probably do something like: $output = 'i is unknown';
if($i == 0){
return 'i equals 0';
}
if($i == 1) {
return 'i equals 1';
}
if ($i == 2) {
return 'i equals 2';
}
return $output;
Not really, I'd probably do.. if($i) {
return "i equals {$i}";
}
return 'i is unknown';
but the point was if I had to check for multiples to match something, then do something else, that's how I would do it... albeit this is a simple case with a better solution. |
|