Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Switch Statement

    I'm not understand why this doesn't work:

    PHP Code:
    echo '"'$default_class '"';
    // Set ticket price based on chosen class
    switch($default_class) {
        case 
    "coach":
            
    $ticketprice 99.99;
        case 
    "business":
            
    $ticketprice 149.99;
        case 
    "first":
            
    $ticketprice 199.99;
        default:
            
    $ticketprice 89.99;
        }
    echo 
    '"'$ticketprice '"'
    It echos the right $default_class, but doesn't set the right ticket price. It always defaults to 89.99.

    Any ideas?

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You forgot to add breaks after the cases.

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    61
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by epheterson View Post
    I'm not understand why this doesn't work:

    PHP Code:
    echo '"'$default_class '"';
    // Set ticket price based on chosen class
    switch($default_class) {
        case 
    "coach":
            
    $ticketprice 99.99;
        case 
    "business":
            
    $ticketprice 149.99;
        case 
    "first":
            
    $ticketprice 199.99;
        default:
            
    $ticketprice 89.99;
        }
    echo 
    '"'$ticketprice '"'
    It echos the right $default_class, but doesn't set the right ticket price. It always defaults to 89.99.

    Any ideas?
    As Inigoesdr said you need to add the breaks in. So it'd look like:

    PHP Code:
    echo '"'$default_class '"';
    // Set ticket price based on chosen class
    switch($default_class) {
        case 
    "coach":
            
    $ticketprice 99.99;
             break;
        case 
    "business":
            
    $ticketprice 149.99;
             break;
        case 
    "first":
            
    $ticketprice 199.99;
             break;
        default:
            
    $ticketprice 89.99;
             break;
        }
    echo 
    '"'$ticketprice '"'
    just incase you haven't done it already.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •