Thuta Learning
IntermediateProgrammingbeginner

Switch

Relax. We'll talk through this in plain words — no textbook voice.

switch statement checks one value against multiple cases — it's great for handling things like roles, statuses, selected menu items, or payment states.

php
<?php
$status = "paid";

switch ($status) {
  case "pending":
    echo "Payment is still pending.";
    break;
  case "paid":
    echo "Payment received. Prepare the order.";
    break;
  case "cancelled":
    echo "Order was cancelled.";
    break;
  default:
    echo "Unknown payment status.";
}
?>
You should see
Payment received. Prepare the order.

Easy traps

  • If a case value's spelling doesn't match exactly, it can fall through to default.
Switch | Thuta Learning