Skip to main content

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.

Comments

  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

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025

 Top 50 Most Asked MERN Stack Interview Questions and Answers for 2025 Now a days most of the IT Company asked NODE JS Question mostly in interview. I am creating this article to provide help to all MERN Stack developer , who is in doubt that which type of question can be asked in MERN Stack  then they can learn from this article. I am Shiva Gautam,  I have 15 Years of experience in Multiple IT Technology, I am Founder of Shiva Concept Solution Best Programming Institute with 100% Job placement guarantee. for more information visit  Shiva Concept Solution 1. What is the MERN Stack? Answer : MERN Stack is a full-stack JavaScript framework using MongoDB (database), Express.js (backend framework), React (frontend library), and Node.js (server runtime). It’s popular for building fast, scalable web apps with one language—JavaScript. 2. What is MongoDB, and why use it in MERN? Answer : MongoDB is a NoSQL database that stores data in flexible, JSON-like documents. It...