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 5 of 5
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,368
    Thanks
    262
    Thanked 32 Times in 31 Posts

    switch question re endswitch

    Hi is there a real benefit to using endswitch

    as an example i guess if the case ends with : then you an use an alternative statment but make sure that alternative ends with ;

    i think i have the above text correct.

    But is there any real benefit to this below using endswitch; or is it better to just use a break; after everything including the default?

    PHP Code:
    switch ($_POST['action']) 
    {
    case 
    'approve':
                         
    $val 'something';
    break;
    case 
    'pending':
                         
    $val 'somethingelse';
    break;
    default:
                         
    $val 'somevalue';
    endswitch;                                                                     
    }
    //close switch 
    thanks
    Last edited by durangod; 09-07-2013 at 10:16 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,281
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Never used it once and wasn't even aware of it until I read this post. Thats how important it is.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    durangod (09-07-2013)

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,368
    Thanks
    262
    Thanked 32 Times in 31 Posts
    lol tango i never had either..

    but just in case you like to read for the heck of it.. here is a link

    http://www.php.net/manual/en/control...ive-syntax.php

    thanks so much i wont use it either

  • #4
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,208
    Thanks
    7
    Thanked 268 Times in 267 Posts
    It is under the "alternative syntax" structures, that most never use. The syntax looks like:
    Code:
    switch ($i):
        case 0:
            echo "i equals 0";
            break;
        case 1:
            echo "i equals 1";
            break;
        case 2:
            echo "i equals 2";
            break;
        default:
            echo "i is not equal to 0, 1 or 2";
    endswitch;
    and the use of endswitch here is to replace the closing brace that normally would be there in the regular syntax.

  • Users who have thanked tracknut for this post:

    durangod (09-07-2013)

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,008
    Thanks
    2
    Thanked 311 Times in 303 Posts
    All of the 'alternative' syntax forms take more typing than the {} form; add clutter to the code; are harder to recognize the matching opening/closing symbols around blocks of code (because they aren't symbols any more, they are keywords); and aren't supported by most syntax highlighting.

    I'm not sure there is any good reason for them to exist. Perhaps if you were using an automated 'code' generator, it would make it easier for it to 'find' the matching closing keyword for any opening statement, but that's the only thing I can think of. There's no advantage in using them when a human is writing/reading the code.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    durangod (09-07-2013)


  •  

    Posting Permissions

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