Snack Time with PHP: A Switch Statement Treat
Want to add a fun twist to your PHP code? Use a switch statement to categorize your favorite snacks! Here’s a basic example:
PHP Code
$snack = “apple”;
switch ($snack) {
case “apple”:
echo “Crunchy and healthy!”;
break;
case “chips”:
echo “Salty and satisfying!”;
break;
case “cookies”:
echo “Sweet and delightful!”;
break;
default:
echo “Yum! What kind of snack is that?”;
}
This code checks the value of the $snack variable. Depending on the snack (apple, chips, cookies), it displays a different message. The default case handles anything not explicitly defined.
Here’s another example for know more about switchcase statement.
Output