PHP Switch বিবৃতি

সুইচ স্ট্যাটমেন্ট বিভিন্ন শর্ত অনুযায়ী ভিন্ন কাজ করার জন্য ব্যবহৃত হয়。

সুইচ স্ট্যাটমেন্ট

আপনি কোনও কোড ব্লককে বেছে নিয়ে কাজ করতে চান তবে, সুইচ স্ট্যাটমেন্ট ব্যবহার করুন。

সুইচ স্ট্যাটমেন্ট ব্যবহার করে লম্বা if..elseif..else কোড ব্লকটি এড়ানো যেতে পারে。

Syntax

switch (expression)
{
case label1:
  Code to be executed when expression = label1 ;
  break;  
case label2:
  Code to be executed when expression = label2 ;
  break;
default:
  Code to be executed when the value of the expression is not equal to label1 and label2;
}

Working Principle:

  1. Perform a single calculation of the expression (usually a variable)
  2. Compare the value of the expression with the value of the case in the structure
  3. If there is a match, execute the code associated with the case
  4. After the code is executed,break statementPrevent the code from jumping into the next case and continuing to execute
  5. If no case is true, use the default statement

Example

<?php
$favfruit="orange";
switch ($favfruit) {
   case "apple":
     echo "Your favorite fruit is apple!";
     break;
   case "banana":
     echo "Your favorite fruit is banana!";
     break;
   case "orange":
     echo "Your favorite fruit is orange!";
     break;
   default:
     echo "Your favorite fruit is neither apple, banana, or orange!";
}
?>

Run Example