Home  >  Article  >  Backend Development  >  Why do some PHP frameworks choose if ():else: endif to limit the range instead of {}

Why do some PHP frameworks choose if ():else: endif to limit the range instead of {}

WBOY
WBOYOriginal
2016-08-04 09:20:591184browse

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 ^_^

Reply content:

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn