Php – Switch Case Conditional Statements


[rps-include post=6522]

In previous post we have learned how if-elseif-else conditional statement works. Php provides one more conditional statement named switch-case . switch-case statements generally used for more precise and deterministic decisions. We generally check if given value fully matches with given single value in each statement.

Syntax of switch-case is like below.

switch(VARIABLE){

    case LABEL1:
        CODE
        break;
    case LABEL2:
        CODE
        break;
    ...
}

As we can see from syntax we provide VARIABLE and look for each case if LABEL matches with VARIABLE . If matches the CODE is executed and break finishes case code block and exits switch .

$status="Open";

switch($today){

    case "Open":
        echo "Open";
        break;

    case "Closed":
        echo "Closed";
        break;
}

This example will print Open because the case condition matches with it.

[rps-include post=6522]

LEARN MORE  How to Configure Cisco Device and User Modes

Leave a Comment