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

后端 未结 6 809
暗喜
暗喜 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 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:

    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:

    
    
    This always happens!

    This is going to cause an infinite loop!

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

    
    
    This always happens!

    This is going to cause an infinite loop!

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

提交回复
热议问题