Responsive Advertisement

Explain switch with the help of a program.

 Explain switch with the help of a program.


     

The `switch` statement in PHP is used to perform different actions based on 
different conditions. It provides an alternative way to handle multiple conditions 
instead of using multiple `if...elseif...else` statements. Here's an example 
program to demonstrate the usage of the `switch` statement:
```php
<?php
 $day = "Monday";
 switch ($day) {

case "Monday": 
 echo "Today is Monday.";
 break; case "Tuesday":
 echo "Today is Tuesday."; 
 break; case "Wednesday": 
 echo "Today is Wednesday.";
 break; default: 
 echo "Today is not Monday, Tuesday, or Wednesday."; 
 break; 
 }
 ?> ```
In the above example, the value of the variable `$day` is compared with different cases using the `switch` statement. If a match is found, the corresponding code block is executed until a `break` statement is encountered, which terminates the `switch` statement. In this case, since the value of `$day` is "Monday," the code block under the case "Monday" is executed, and the output will be "Today is Monday." If the value of `$day` were "Tuesday," the code block under the case "Tuesday" would execute, and so on

Post a Comment

0 Comments