PHP switch Statement

Posted by Mix 2016-02-28 15:39:00 View: 590

PHP switch Statement

PHP switch Statement กำหนดเงื่อนไขและทางเลือกที่เป็นจริงได้หลายเงื่อนไข

Syntax

switch (n) {
    case label1:
        //code to be executed if n=label1;
        break;
    case label2:
        //code to be executed if n=label2;
        break;
    case label3:
        //code to be executed if n=label3;
        break;
    ...
    default:
        //code to be executed if n is different from all labels;
}

Example

<?php   
$i=1;
switch ($i) {
    case 0:
         echo 'The value is 0';
         break;
    case 1:
         echo 'The value is 1';
         break;
    case 2:
         echo 'The value is 2';
         break;
    case 3:
         echo 'The value is 3';
         break;
    default:
         echo 'The value is none';
}

//output = The value is 1
?>