Closed Thread
Results 1 to 9 of 9

PHP Switch Statement???

This is a discussion on PHP Switch Statement??? within the PHP, CGI, ASP, Server-Side Programming forums, part of the Webmaster / Coding / Web Design Discussion category; Switch statement should be used in the code when you want to evaluate different cases of a given scenario. The ...

  1. #1
    New Hunter austin47 is on a distinguished road
    Join Date
    Jan 2010
    Location
    U.K
    Posts
    2

    Default PHP Switch Statement???

    Switch statement should be used in the code when you want to evaluate different cases of a given scenario. The switch statement takes a single variable as input and then checks it against all cases we have in our switch statement.
    Let's imagine you own a computer retail store. Depending on the product name we want to display the price of that product.
    Now, instead writing separate if else statements for each product name, we simple use the PHP switch statement on the product name variable to evaluate which product we want to the display the price for. Let's have look at the example.
    PHP Code:
    <html>
    <body>

    <?php
    //set product name
    $product_name "Processors";

    switch (
    $product_name)
    {
        case 
    "Video Cards":
            echo 
    "Video cards range from $50 to $500";
            break;
        case 
    "Monitors":
            echo 
    "LCD monitors range from $200 to $400";
            break;
        case 
    "Processors":
            echo 
    "Intel processors range from $100 to $1000";
            break;
        default:
            echo 
    "Sorry, we don't carry $product_name in our catalog";
            break;
    }
    ?>

    </body>
    </html>

  2. #2
    New Hunter saviourdlima is on a distinguished road
    Join Date
    May 2010
    Posts
    1

    Default

    PHP switch statement is used when one of the more than a few choice or a groups of options are to be executed based on some situation. We can use several PHP if conditions with PHP if else but this is a better way of coding when we know one of the several options is correct. The PHP code will evaluate the PHP switch statement and its cases and once the code is valid then it will execute the code within it depending on the use of break statement
    Code:
    <?php
    $picture ='church';
    switch ($picture) {
    case 'kitten':
    print('Kitten Picture');
    break;
    case 'church':
    print('Church Picture');
    break;
    }
    ?>

  3. #3
    New Hunter almedajohnson is on a distinguished road
    Join Date
    Sep 2010
    Posts
    1

    Default

    This is the basics of C and everyone knows that switch statement will be used when you have multiple conditions and if-else condition will make code stuff. I think if you want to share something then share something innovative or new so it can be helpful to others rather than sharing this beginners guide.

  4. #4
    New Hunter iamjohnbrown is on a distinguished road
    Join Date
    Sep 2010
    Posts
    2

    Default

    ASP.NET has also the similar thing... Most of the modern programming languages support this syntax. Switch is really helpful most of the times.
    Internet Television

    Live and let live...

  5. #5
    New Hunter amandabyes is on a distinguished road
    Join Date
    Oct 2010
    Posts
    1

    Default

    PHP switch statement gives you result in the form of true or false. Through this you can test for more true and false statement and that also with both simple numbers and strings.

  6. #6
    New Hunter harrygail12 is on a distinguished road
    Join Date
    Dec 2010
    Posts
    2

    Default

    It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example: <?php
    switch ($i) {
    case 0:
    echo "i equals 0";
    case 1:
    echo "i equals 1";
    case 2:
    echo "i equals 2";
    }
    ?>

  7. #7
    New Hunter loyamos is on a distinguished road
    Join Date
    Dec 2010
    Posts
    1

    Default

    Switch statement code in the same statement expression.Switch statement should be used when a given scenario you want to evaluate a range of different cases is similar. PHP switch statement is used when a multiple-choice or choices based on certain conditions for a group to be executed.

  8. #8
    New Hunter benschrist is on a distinguished road
    Join Date
    Dec 2010
    Posts
    1

    Default

    The switch statement takes a single variable as input and then checks it against all cases we have in our switch statement.The PHP code will evaluate the PHP switch statement and its cases and once the code is valid then it will execute the code within it depending on the use of break statement.

  9. #9
    New Hunter smear is on a distinguished road
    Join Date
    Dec 2010
    Posts
    3

    Default

    The switch statement is used in multiple choices and same as the if else statements. so it is convenient in PHP that is why most people prefer it.

Closed 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