When do I use if/endif versus If{}?

后端 未结 6 807
暗喜
暗喜 2021-02-08 08:30

Well the question is self explanatory.

In PHP when do I use the if/endif notation instead of the standard if(something){} notation?

Exa

相关标签:
6条回答
  • 2021-02-08 08:58

    I mostly use if/endif when stopping and starting the php code block in between, so nearly always when raw html is being outputted.

    For example:

    <?php if ($n == 3): ?>
    Lorem ipsum.
    <?php endif; ?>
    

    This looks, at least in my opinion, better than the following:

    <?php if ($n == 3) { ?>
    Lorem ipsum.
    <?php } ?>
    
    0 讨论(0)
  • 2021-02-08 09:00

    Both methods are acceptable. Some people maintain that the alternative syntax (endif) is more legible in templates. IMHO, with a modern syntax coloring/highlighting error, that no longer holds true. Just pick a style and stick with it.

    0 讨论(0)
  • 2021-02-08 09:01

    It is a stylistic choice. They are analogous and one is not better than another.

    The former can be good in certain circumstances because it may be easier to read when mired in the middle of an HTML block set.

    0 讨论(0)
  • 2021-02-08 09:09

    Short answer:

    Always use if($something) { } and pretty much never use if/endif. It's standard since PHP 4.0.0 and while the old syntax is not going away this is the one everybody expects.

    If you are really looking for a case where you can use if/endif then it's when using php as a template language.

    Some people like something like this:

    <?php if ($foo): ?>
    Hi There
    <?php else: ?>
    cya!
    <?php endif; ?>
    

    better than

    <?php if ($foo) { ?>
    Hi There
    <?php } else { ?>
    cya!
    <?php } ?>
    

    and imho it is because every line "speaks" and that can be helpful when there is a lot of html inbetween the php tags.

    0 讨论(0)
  • 2021-02-08 09:10

    Others have given the answer "for templating", but haven't really explained why. Curly braces are great for denoting blocks, but they kind of rely on indentation to be read clearly. So this is fairly clear:

    <?php
    if (1 == 2)
    {
        while (1 < 2)
        {
            doSomething();
        }
    }
    

    It's obvious which brace matches which.

    If, however, you're going into an HTML block, you're probably going to stop indenting cleanly. For instance:

    <?php
    if (1 != 2) { ?>
    <div>This always happens! <?php while (true) { ?>
      <p>This is going to cause an infinite loop!</p>
    <?php }
    }
    

    That's hard to follow. If you use the endif style, it looks a little cleaner:

    <?php
    if (1 != 2): ?>
    <div>This always happens! <?php while (true): ?>
      <p>This is going to cause an infinite loop!</p>
    <?php endwhile;
    endif;
    

    The content of the code shows more clearly what's being done. For this limited case, it's more legible.

    0 讨论(0)
  • 2021-02-08 09:11

    You can use both, but preffered style and currently known style is the standard if(expr).

    0 讨论(0)
提交回复
热议问题