Switch Statement In PHP:-

15

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.
Tags

Post a Comment

15Comments

POST Answer of Questions and ASK to Doubt

  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
  8. Greater Number Using Switch
    Shubham Nandwal

    $a=1002;
    $b=2000;
    $c=2510;
    switch($a)
    {
    case $a>$b && $a>$c;
    echo "a is greater";
    break;
    case $b>$c;
    echo "b is greater";
    break;
    default:
    echo "c is greater";
    break;
    }

    ReplyDelete
  9. Even Odd Using Switch
    Shubham Nandwal

    $a=10;

    switch($a)
    {
    case $a%2==0;
    echo "even number";
    break;
    default:
    echo "odd number";
    break;
    }

    ReplyDelete
  10. Vowel Consonant Using Switch
    Shubham Nandwal

    $a='A';

    switch($a)
    {
    case 'a';
    echo "vowel";
    break;
    case 'e';
    echo "vowel";
    break;
    case 'i';
    echo "vowel";
    break;
    case 'o';
    echo "vowel";
    break;
    case 'u';
    echo "vowel";
    break;
    case 'A';
    echo "vowel";
    break;
    case 'E';
    echo "vowel";
    break;
    case 'I';
    echo "vowel";
    break;
    case 'O';
    echo "vowel";
    break;
    case 'U';
    echo "vowel";
    break;
    default:
    echo "consonant";
    break;

    ReplyDelete
  11. switch calculator
    Shubham Nandwal

    $n="/";
    $a=115;
    $b=100;
    switch($n)
    {
    case $n=="+";
    $x=$a+$b;
    echo "ADDITION OF TWO NUMBER IS = " .$x;
    break;
    case $n=="-";
    $x=$a-$b;
    echo "SUBTRACTIN OF TWO NUMBER IS = " .$x;
    break;
    case $n=="*";
    $x=$a*$b;
    echo "MULTIPLICATION OF TWO NUMBER IS = " .$x;
    break;
    case $n=="/";
    $x=$a/$b;
    echo "DIVISION OF TWO NUMBER IS = " .$x;
    break;
    default:
    echo "ENTER A VALID SYMBOL";
    break;
    }

    ReplyDelete
  12. $id = $_REQUEST['txt1'];
    $name = $_REQUEST['txt2'];
    $mobile = $_REQUEST['txt3'];
    $mail = $_REQUEST['txt4'];
    $conn= mysqli_connect('localhost','root','','prphp');
    $res= mysqli_query($conn,"insert into phptabl(id,name,mobile,email)
    value('$id','$name','$mobile','$mail')");
    if ($res>0)
    {
    echo "successfully";
    }
    else
    {
    echo "failed";
    }

    ReplyDelete
  13. ishu manglam
    Yes,NO and Cancel using switch

    $option = 'k';
    switch($option)
    {
    case 'y':
    echo "YES";
    break;

    case 'n':
    echo "NO";
    break;

    case 'c':
    echo "CANCEL";
    break;

    default:
    echo "INPUT IS WRONG";
    break;

    }

    ReplyDelete
  14. mohammad kabir



    $first=$_POST['txt1'];
    $second=$_POST['txt2'];
    $oprator=$_POST['submit'];
    $result='';
    if (is_numeric($first)&& is_numeric($second))
    {

    switch ($oprator)
    {
    case "add":
    $result=$first+$second;
    break;
    case "subtract":
    $result=$first-$second;
    break;
    case "multiply":
    $result=$first*$second;
    break;
    case "divid":
    $result=$first/$second;
    break;
    }
    }


    < action="" method="post">

    first number
    < type="number" name="txt2" placeholder="inter se number" value="">second number

    < readonly placeholder="see answer here" value="">result
    < type="submit" name="submit" value="add">
    < type="submit" name="submit" value="subtract">
    < type="submit" name="submit" value="multiply">
    < type="submit" name="submit" value="divid">


    ReplyDelete
Post a Comment