简体   繁体   English

大量打开/关闭PHP标签

[英]A lot of opening/closing of PHP tags

I've always wondered: does having a large open and close PHP (ie a template) hurt? 我一直想知道:拥有大量打开和关闭PHP(即模板)是否会受伤?

To clarify: 澄清:

 <html>
    <?php echo $test; ?>
    <body <?php echo $test2; ?>>
    <table>
        <?php foreach ($rows as $row): ?>
        <tr>
            <td><?php echo $row['cell1']; ?></td>
            <td><?php echo $row['cell1']; ?></td>
            <td><?php echo $row['cell1']; ?></td>
            <td><?php echo $row['cell1']; ?></td>
            <?php echo $row['added cells']; ?>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php echo $someMorePhp; ?>
    <div>
        <?php /*do some more php stuff */ ?>
    </div>
    etc etc
    etc

Or would it be advisable to ie 还是建议

<html>
<php echo $test.'<body'.$test2; ?>>
<table>
    <?php foreach ($rows as $row) {
    echo '<tr>
        <td>'.$row['cell1'].'</td>
        <td>'.$row['cell2'].'</td>
        <td>'.$row['cell3'].'</td>
        <td>'.$row['cell4'].'</td>
        <td>'.$row['cell1'].'</td>
        '.$row['added cells'].'
    <tr>';
    }
</table>
// etc

I know this might seem like a micro optimization and such. 我知道这看起来像是微优化等等。 To be clear i'm looking for a rule-of-thumb not a specific usecase... Will it hurt entering and exiting the php engine during a single script run... 明确地说,我正在寻找一种经验法则,而不是特定的用例...在单个脚本运行期间进入和退出php引擎是否会受到伤害...

No, that is no problem, you should always opt for better readability in this case. 不,那没问题,在这种情况下,您应该始终选择更好的可读性。

Also think about either activating shorttags if possible (but beware, those might have some sideeffects / problems with XML sytnax as stated in the comments) 还请考虑激活短标签(如果可能的话)(但请注意,如注释中所述,短标签可能会产生一些副作用/ XML sytnax问题)

<? if ($bool) { ?>
    <?= $myVarThatIWantToOutput ?>
<? } ?>

Or think about using a template engine like smarty or twig, which restrict you somehow to force splitting of concerns, make some stuff easier (and more readable) and allow caching of compiled templates to make sure you still get the right speed. 或者考虑使用诸如smarty或细枝之类的模板引擎,这些模板引擎会以某种方式限制您进行关注点拆分,使某些内容更容易(并且更具可读性),并允许缓存已编译的模板,以确保您仍然获得正确的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM