B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP PHP switch | LiveCodeProgramming

PHP switch Statement

The switch statement in PHP is used to perform different actions based on different conditions (like multiple if...else).

PHP में switch स्टेटमेंट का उपयोग कई स्थितियों के आधार पर विभिन्न क्रियाएं करने के लिए किया जाता है (जैसे कई if...else स्टेटमेंट)।

Basic switch Example

Choose a case based on a value.

एक वैल्यू के आधार पर केस चुना जाता है।

<?php
$day = "Monday";

switch ($day) {
  case "Monday":
    echo "Start of the week";
    break;
  case "Friday":
    echo "Almost weekend";
    break;
  default:
    echo "Midweek day";
}
?>
Start of the week

switch with Numbers

Use numbers in switch condition.

संख्याओं के साथ switch का प्रयोग करें।

<?php
$grade = 2;

switch ($grade) {
  case 1:
    echo "Excellent";
    break;
  case 2:
    echo "Very Good";
    break;
  case 3:
    echo "Good";
    break;
  default:
    echo "Try Again";
}
?>
Very Good

switch without break (Fallthrough)

If no break, next cases also run.

break नहीं होने पर अगले केस भी चलेंगे।

<?php
$fruit = "Apple";

switch ($fruit) {
  case "Apple":
    echo "It's red. ";
  case "Banana":
    echo "It's yellow. ";
  default:
    echo "Fruit selected.";
}
?>
It's red. It's yellow. Fruit selected.