DEV Community

Discussion on: PHP 8 features I wish also existed in JavaScript

Collapse
 
devdufutur profile image
Rudy Nappée • Edited


$fontWeight = match ($weight) {
100 => "Super Thin",
300 => "Thin",
400 => "Normal",
600 => "Bold",
900 => "Heavy"
};

Error prone code... What's the value of $fontWeight if $weight = 101 ? Is there an error ?

Safe "Switch expressions" constructs should assert there is always a default clause (i'm looking at you rust 😍)

Collapse
 
devmount profile image
Andreas • Edited

match has a default case too. Also, you can comma-separate multiple values:

$fontWeight = match ($weight) {
  100, 200 => "Super Thin",
  300 => "Thin",
  400, 500 => "Normal",
  600, 700, 800 => "Bold",
  900 => "Heavy",
  default => "Not valid"
};
Enter fullscreen mode Exit fullscreen mode

Edited the example in the article accordingly.