التخطي إلى المحتوى الرئيسي

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.

تعليقات

  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");
    }
    ?>

    ردحذف
  2. Kritika Barod


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

    ?>

    ردحذف
  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;
    }
    ?>

    ردحذف
  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;
    ?>

    ردحذف
  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;

    }
    ?>

    ردحذف
  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;
    }
    ?>

    ردحذف
  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 !";
    }

    ?>

    ردحذف
  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;
    }

    ردحذف
  9. Even Odd Using Switch
    Shubham Nandwal

    $a=10;

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

    ردحذف
  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;

    ردحذف
  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;
    }

    ردحذف
  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";
    }

    ردحذف
  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;

    }

    ردحذف
  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">


    ردحذف

إرسال تعليق

POST Answer of Questions and ASK to Doubt

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...