← Back to PHP Course | Chapter 4: Control Flow | Lesson 7 of 14

PHP switch Statement

एक switch एक vending machine keypad जैसा है: आप एक code press करते हैं और यह matching item देता है। PHP एक value को choices की एक list के against compare करता है और जो match करे वह चलाता है।
Syntax
php
switch ($variable) {
    case value1:
        // ...
        break;
    case value2:
        // ...
        break;
    default:
        // no case matched
}

Basic switch Statement

switch एक variable को fixed candidate values की एक list के against compare करता है, जो एक बार आप उसी variable के against दो या तीन से ज़्यादा specific values check कर रहे हों तो एक equivalent elseif ladder से ज़्यादा cleanly पढ़ता है।

उदाहरण: Basic switch Statement

php
<?php
// Declare `$day`, set to "Tuesday"
$day = "Tuesday";
// Switch on `$day` and branch by matching case
switch ($day) {
    // Case: `"Monday"`
    case "Monday":
        // Print "Start of week" to the output
        echo "Start of week";
        break;
    // Case: `"Tuesday"`
    case "Tuesday":
        // Print "Second day" to the output
        echo "Second day";
        break;
}
?>

break का महत्व

बिना एक explicit break के, execution एक match मिलने के बाद अगले case के code में 'fall through' हो जाता है, इसे भी चलाते हुए भले ही इसकी अपनी condition कभी check ही न हुई हो — bugs का एक frequent source, और मुख्य वजह जिससे break लगभग हर case के अंत में mandatory माना जाता है।

उदाहरण: The Importance of break

php
<?php
// Declare `$day`, set to "Monday"
$day = "Monday";
// Switch on `$day` and branch by matching case
switch ($day) {
    // Case: `"Monday"`
    case "Monday":
        // Print "Monday\n" to the output
        echo "Monday\n";
    // Case: `"Tuesday"`
    case "Tuesday":
        // Print "Tuesday (falls through!)\n" to the output
        echo "Tuesday (falls through!)\n";
        break;
}
?>

Default Switch Case

default तब चलता है जब tested value listed किसी भी case से match नहीं करती — functionally वही role जो else एक elseif ladder के अंत में निभाता है, ऊपर explicitly handle न हुई हर चीज़ को पकड़ते हुए।

उदाहरण: Default Switch Case

php
<?php
// Declare `$fruit`, set to "mango"
$fruit = "mango";
// Switch on `$fruit` and branch by matching case
switch ($fruit) {
    // Case: `"apple"`
    case "apple":
        // Print "Apple" to the output
        echo "Apple";
        break;
    // Default case, if nothing above matched
    default:
        // Print "Unknown fruit" to the output
        echo "Unknown fruit";
}
?>

Switch Cases को Group करना

बीच में कोई code न रखते हुए कई case labels को एक के बाद एक list करना, इसके बाद एक single shared block, कई distinct values को हर एक के लिए block दोहराए बिना identical behavior trigger करने देता है।

उदाहरण: Grouping Switch Cases

php
<?php
// Declare `$day`, set to "Saturday"
$day = "Saturday";
// Switch on `$day` and branch by matching case
switch ($day) {
    // Case: `"Saturday"`
    case "Saturday":
    // Case: `"Sunday"`
    case "Sunday":
        // Print "Weekend" to the output
        echo "Weekend";
        break;
    // Default case, if nothing above matched
    default:
        // Print "Weekday" to the output
        echo "Weekday";
}
?>

Alternative Colon Syntax

if की तरह, switch एक colon-based alternative syntax support करता है जो endswitch; से बंद होता है, जो PHP और HTML mix करने वाले templates को default brace-heavy form से ज़्यादा readable रखता है।

उदाहरण: Alternative Colon Syntax

php
<?php
// Declare `$role`, set to "admin"
$role = "admin";
// Switch on `$role` and branch by matching case
switch ($role):
    // Case: `"admin"`
    case "admin":
        // Print "Admin panel" to the output
        echo "Admin panel";
        break;
    // Default case, if nothing above matched
    default:
        // Print "No access" to the output
        echo "No access";
endswitch;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. break भूल जाना, ताकि execution fall through हो जाए और following case का code भी चला दे।
  2. यह उम्मीद करना कि switch strictly compare करेगा, जबकि यह == इस्तेमाल करता है, इसलिए "0" और 0 same case से match कर सकते हैं।
  3. default छोड़ देना, ताकि unexpected values चुपचाप कुछ न करें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.