Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Switch Statement In PHP-
..............................................................................................................................................



It is used to create option based or choice-based program.we can choose or select a particular option in options set.


Switch contains multiple cases to define multiple options.

switch(option)       //option==optionvalue
{
         case optionvalue:
              optionstatement;
              break;
        case optionvalue:
              optionstatement;
              break;
        case optionvalue:
              optionstatement;
              break;

       default:
          statement;
          break;

}


WAP to display "yes","no" and "cancel"?

<?php

$op='L';
$option = (ord($op)>=65 && ord($op)<=91)?chr(ord($op)+32):$op;
switch($option)
{
case 'y':
echo "YES";
break;
case 'n':
echo "NO";
break;
case 'c':
echo "CANCEL";
break;
default:
echo "INVALID INPUT";
break;
}





?>

ASSIGNMENT:-

1)  WAP to check the greater number using Switch?

2)  WAP to check even or odd using Switch?

3) WAP to check vowel and Consonant using Switch?

4)  WAP to calculate addition, subtraction, multiplication, and division when user press +,-,*, and / Symbol.






8 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. Kritika Barod

    ";
    echo ("a=$a");
    echo ("
    b=$b");
    switch($option)
    {
    case 1:
    if($a>$b)
    {
    echo("
    a is greater than b");
    }
    else
    {
    echo("
    b is greater than a");
    }
    break;
    default;
    echo("invalid");
    }
    ?>

    ReplyDelete
  2. Kritika Barod


    given num is even");
    }
    else
    {
    echo ("
    given num is odd");
    }
    break;
    }

    ?>

    ReplyDelete
  3. Kritika Barod


    $char is vowel";
    break;
    case 'e':
    echo "
    $char is vowel";
    break;
    case 'i':
    echo "
    $char is vowel";
    break;
    case 'o':
    echo "
    $char is vowel";
    break;
    case 'u':
    echo "
    $char is vowel";
    break;
    case 'A':
    echo "
    $char is vowel";
    break;
    case 'E':
    echo "
    $char is vowel";
    break;
    case 'I':
    echo "
    $char is vowel";
    break;
    case 'O':
    echo "
    $char is vowel";
    break;
    case 'U':
    echo "
    $char is vowel";
    break;
    default:
    echo "
    $char is consonant";
    break;
    }
    ?>

    ReplyDelete
  4. Kritika Barod


    ";
    echo "1 Addition
    ";
    echo "2 Subtraction
    ";
    echo "3 multiplication
    ";
    echo "4 Divison
    ";
    $ch='3';
    echo" given no is $a and $b
    ";
    echo "your choice is $ch
    ";
    switch($ch)
    {
    case 1:
    $r = $a+$b;
    echo " Addition = ".$r ;
    break;
    case 2:
    $r = $a-$b;
    echo " Subtraction = ".$r ;
    break;
    case 3:
    $r = $a*$b;
    echo " Multiplication = ".$r ;
    break;
    case 4:
    $r = $a/$b;
    echo " Divison = ".$r ;
    break;
    default:
    echo ("invalid option\n");
    }
    return 0;
    ?>

    ReplyDelete
  5. ";
    echo "
    ";

    $a=300;
    $b=500;

    if($a>$b)
    $greater=$a;
    else
    $greater=$b;

    switch($greater)
    {
    case "$a";
    echo "Answer 'a' is greater number";
    break;
    default:
    echo "Answer 'b' is greater number";
    break;

    }
    ?>

    ReplyDelete
  6. ";
    echo "
    ";

    $num=99;

    echo "Number is- [ $num ]";

    echo "
    ";
    echo "
    ";

    if($num%2==0)
    $num="even";
    else
    $num="odd";

    switch($num)
    {
    case "even";
    echo " This is a even number";
    break;

    default:
    echo "This is a odd number";
    break;
    }
    ?>

    ReplyDelete
  7. ";
    echo "
    ";

    $char='a';

    switch($char)
    {
    case 'a';
    echo "$char is vowel !";
    break;
    case 'e';
    echo "$char is vowel !";
    break;
    case 'i';
    echo "$char is vowel !";
    break;
    case 'o';
    echo "$char is vowel !";
    break;
    case 'u';
    echo "$char is vowel !";
    break;

    case 'A';
    echo "$char is vowel !";
    break;
    case 'E';
    echo "$char is vowel !";
    break;
    case 'I';
    echo "$char is vowel !";
    break;
    case 'O';
    echo "$char is vowel !";
    break;
    case 'U';
    echo "$char is vowel !";
    break;

    default:
    echo "$char is consonant !";
    }

    ?>

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post