PHP Switch Loop In Hindi

📔 : PHP 🔗

PHP में Switch Loop , किसी matched expression के लिए code of block Run करता है , यह Else If  की तरह ही work करता है जहा हम कई सारी  Conditions में से True Condition वाला statement ही Run होता था,  और अगर एक भी condition  match नहीं होती तो else part (default) run होता था। वहीँ Switch में हम cases use करते  हैं , और जो case match करता है वही statement execute करता है। और कोई case match न  होने पर default statement execute होता है।

PHP Switch Loop Syntax

switch (expression) 
{
    case valueN:
        echo "case 1";
        break;
    case valueN:
        echo "Case 2";
        break;
    default:
        echo "default case";
}

  • expression - दिया गया expression , case में दी गयी value से match होता है।
  • case clause - एक switch loop में कितने ही case clause हो सकते हैं , और case में दी जाने वाली value दिए गए expression से match करती है अगर expression match होता है ,तो उस case से associated code of block रन हो जाता है।
  • break - expression match होने पर उस case से associated code of block रन होने के बाद break statement switch loop को terminate करता है।  learn more about break . . .
  • default - अगर कोई भी expression match नहीं होता है तो default statement run होता है।

PHP Switch Loop Example

File : switch.php

Copy Fullscreen Close Fullscreen
<?php   
  $x = "apple";
  switch ($x) 
  {
      case "apple":
          echo "x is apple";
          break;
      case 3:
          echo "x is bar";
          break;
      case 9:
          echo "x is cake";
          break;
      default:
          echo "default case running";
  }   
?>
Output
x is apple

Note - जैसा कि आप example में देख रहे होंगे कि हर case statement के end में break keyword का use किया गया है , जिससे कि matched case statement ही रन हो अगर हम ये break remove तो जिस case के साथ value match करती है वहाँ से सभी statements (cases) switch Loop ख़त्म होने तक execute होंगे।

See Example :

File : switch2.php

Copy Fullscreen Close Fullscreen
<?php   
    $x = 3;
    switch ($x) 
    {
        case "apple":
            echo "x is apple";
        case 3:
            echo "x is bar <br>";
        case 9:
            echo "x is cake <br>";
        default:
            echo "default case running";
    } 
?>
Output
x is bar
x is cake
default case running

तो देखा आपने कि break keyword remove करने पर जो case match होता है वहाँ से सभी statements (default statement ) execute होते है।

Note - हालाँकि अगर हम case के बाद use किये जाने वाले colon ( : ) की जगह semicolon ( ; ) use करते हैं तो भी कोई problem नहीं है।

  <?php
    /*It is valid, we can use semicolon instead of colon*/
     $x = 3;
    switch ($x) 
    {
        case "apple";
            echo "x is apple";
        case 3;
            echo "x is bar <br>";
        case 9;
            echo "x is cake <br>";
        default;
            echo "default case running";
    }  
?>

A Very Important Note - हो सके तो switch loop में mixed type value जैसे 1str , str324 use करने से बचें , इससे problem हो सकती है।

For Example :

File : switch3.php

Copy Fullscreen Close Fullscreen
<?php  
  $string="1string";
  switch($string)
  {
      case 1:
          echo "this is 1";
          break;
      case 2:
          echo "this is 2";
          break;
      case '1string':
          echo "this is a string";
          break;
      default:
        echo "Default case";
  } 
?>
Output
this is 1

तो जैसा आप देख सकते हैं कि variable में 1string assign किया गया है , और यह case 1 , से ही match कर ले रहा है वजाय case '1string' के , इसलिए ये Switch Loop में mixed type value use करने से बचें।


Important
ये Examples PHP Version 7.4 पर run किये गए थे , हालाँकि PHP Version 8 में matching rules को strict किया है , अब आप mixed type भी देंगे तब भी कोई problem नहीं है because PHP Version 8 में pass की गयी value के type के according ही case match होता है।

इसके अलावा PHP Version 8 में एक और Match expression add किया गया है जो switch Loop की तरह ही work करता है , but switch loop से काफी sort & easy to understand है।

PHP Match expression

File : php_8_match_exp.php

Copy Fullscreen Close Fullscreen
<?php  
/*try it on PHP 8*/
echo match(true)
{
0 => 'this is zero',
false => 'False',
true => 'True',
'1' => 'This is string 1',
1 => 'This is int 1',
};
?>
Output
True 

हालाँकि इसमें comma separated multiple values से भी match करा सकते हैं।
For Example :

<?php
/*try it on PHP 8*/
echo match (4) {
1, 2 => 'Same for 1 and 2',
3, 4 => 'Same for 3 and 4',
};

/*Output : Same for 3 and 4*/
?>

Read Official Doc About Match Expression

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers