Home >Backend Development >PHP Tutorial >Why do some PHP frameworks choose if ():else: endif to limit the range instead of {}
Why some php frameworks choose
<code><?php if(): // some code endif; ?></code>
to limit the range instead of using
<code><?php if(){ //some code } ?></code>
//---- Additional question ----
Is there any performance difference between the two?
After reading the answers of several students, whether they are clear or not, I feel that both are quite clear ^_^
Why some php frameworks choose
<code><?php if(): // some code endif; ?></code>
to limit the range instead of using
<code><?php if(){ //some code } ?></code>
//---- Additional question ----
Is there any performance difference between the two?
After reading the answers of several students, whether they are clear or not, I feel that both are quite clear ^_^
Generally, the first syntax is more suitable for nesting in HTML.
For example
<code class="php"><?php if($value): ?> <div>aaaaa</div> <?php else: ?> <div>bbbbb</div> <?php endif; ?></code>
Isn’t it better than
<code class="php"><?php if($value){ ?> <div>aaaaa</div> <?php }else{ ?> <div>bbbbb</div> <?php } ?></code>
More clear!
The meaning expressed by the two ways of writing is exactly the same, but they look different. The first one is the same as mentioned above. When there is html code, the statement will appear particularly clear. The second one is usually written in a pure php file. , I think I have a strong sense of logic.
<code> } }</code>
<code> endforeach; endif;</code>
When the code is relatively long, it will be clearer when you look at the tail
But as other answers said, it is more readable when nested in html
Personally I hate this way of writing:
<code>if(): // some code endif;</code>
?>
You use
<code><?php if () { } else { } ?></code>
It’s the same, the effect is the same.