PHP alternative to Brackets

Discussion in 'Web Design & Programming' started by RHochstenbach, Mar 9, 2011.

  1. RHochstenbach

    RHochstenbach Administrator

    Likes Received:
    26
    Trophy Points:
    48
    If you have a lot of IF statements and loops nested into each other, you might find it confusing to see which bracket belongs to which instruction. Something like this:
    PHP:
    if(== 1) {
    while(
    == 1) {
    echo 
    "1";
    if(
    2==2) {
    while(
    2==2) {
    echo 
    "2";
    }
    }
    }
    }
    You can replace the bracket with the word end, followed by the name of the instruction. Examples are endif, endwhile, endfor etc. This gives a result similar to this:

    PHP:
    if(== 1) {
    while(
    == 1) {
    echo 
    "1";
    if(
    2==2) {
    while(
    2==2) {
    echo 
    "2";
    endwhile;
    endif;
    endwhile;
    endif;
    Looks easier to read, doesn't it :)
     
  2. Sniper

    Sniper Administrator Staff Member

    Likes Received:
    59
    Trophy Points:
    63
    I'd recommend using the alternative syntax only when working with templates, otherwise use the normal opening/closing brackets.
     
  3. RHochstenbach

    RHochstenbach Administrator

    Likes Received:
    26
    Trophy Points:
    48
    It's always the best way using the 'official' method with brackets, because these are more compatible with the PHP versions.
     

Share This Page