Results 1 to 6 of 6

Thread: Need Codings for PHP Switch Statement?

  1. #1
    Join Date
    Aug 2006
    Posts
    121

    Need Codings for PHP Switch Statement?

    Can someone tell me why the switch statements are used in PHP. Since, I am very new to PHP, please provide me the detailed information about the same. Also explain me why it is used.? Also please provide me with the Codings for PHP Switch Statement. This is my first time on this forum. So, kindly ignore my mistakes. Hope that someone can help me soon.!!
    Whats my name again? I spend so much time looking at code i fogot my own name ahhh!

  2. #2
    Join Date
    Nov 2008
    Posts
    996

    Re: Need Codings for PHP Switch Statement?

    I am assuming that you have used the if statement in other programming language. Now, in PHP the switch statement is similar to a series of IF statements on the same expression. Hope that you cleared the concept of the Switch Statement in PHP. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. For such conditions the switch statement is used. Also the important thing is that The switch statement executes line by line.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Need Codings for PHP Switch Statement?

    I think that looking at the Syntax, your doubt of the Switch Statements will be solved. The Syntax of the Switch Statements will be like this :
    Code:
    switch (n)
    {
    case text1:
      code to be executed if n=text1;
      break;
    case text2:
      code to be executed if n=text2;
      break;
    default:
      code to be executed if n is different from both text1 and text2;
    }

  4. #4
    Join Date
    Mar 2008
    Posts
    349

    Re: Need Codings for PHP Switch Statement?

    In the following coding, I have tried to explain how the Switch Statement works. Initially we are having a single expression n, which is evaluated once. The value of the expression is then compared with the values for each case in the structure. The block of code associated with that case is executed, if there is a match. Then i have use the break to prevent the code from running into the next case automatically. And if no match is found, then default statement is used. Here is coding for the same :
    Code:
    <html>
    <body>
    
    <?php
    switch ($x)
    {
    case 1:
      echo "Number 1";
      break;
    case 2:
      echo "Number 2";
      break;
    case 3:
      echo "Number 3";
      break;
    default:
      echo "No number between 1 and 3";
    }
    ?>
    
    </body>
    </html>

  5. #5
    Join Date
    Jul 2006
    Posts
    289

    Re: Need Codings for PHP Switch Statement?

    This is the simple example of a coding of Switch Statement :
    Code:
    <?php
    if ($i == 0) {
        echo "i equals 0";
    } elseif ($i == 1) {
        echo "i equals 1";
    } elseif ($i == 2) {
        echo "i equals 2";
    }
    
    switch ($i) {
        case 0:
            echo "i equals 0";
            break;
        case 1:
            echo "i equals 1";
            break;
        case 2:
            echo "i equals 2";
            break;
    }
    ?>
    Signatures reduce available bandwidth

  6. #6
    Join Date
    Aug 2006
    Posts
    235

    Re: Need Codings for PHP Switch Statement?

    In the following example, I have tried you to explain that the Switch Statement allows the use of the strings. Here is an example for illustrate that :
    Code:
    <?php
    switch ($i) {
        case "mango":
            echo "i is mango";
            break;
        case "bike":
            echo "i is bike";
            break;
        case "car":
            echo "i is car";
            break;
    }
    ?>
    3.2 (northwood)
    2gig ram
    ATI AIW X800xt 256mb
    Gigabyte GA-8knxp 875p Chipset
    Optiwrite 8X DVD Burner
    Win XP PRO Sp2 (Works Perfectly)
    2 SATA Raptor 74gig Raid 0
    2 7200 IDE 320gig HD

Similar Threads

  1. How to use switch statement in java
    By Raju Chacha in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 06:40 PM
  2. Java program to use enum in switch statement.
    By MADGE25 in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 06:12 PM
  3. JavaScript Switch Statement
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 03:33 PM
  4. Switch Statement in PHP
    By Projectkmo in forum Software Development
    Replies: 2
    Last Post: 31-03-2009, 12:31 PM
  5. SWITCH....CASE statement in C#
    By Aadarsh in forum Software Development
    Replies: 1
    Last Post: 10-11-2008, 05:39 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,960,745.26626 seconds with 16 queries