Multiple PHP tags

2019-01-20 09:09发布

This is just a general PHP question. Does it help or hurt to have multiple <?php CODE ?> in my scripts?

I was just looking at some of my scripts and noticed that some of them have 3-4 in it. Dont know if this is causing any slowness on my site or not :)

5条回答
贼婆χ
2楼-- · 2019-01-20 09:20

I guess you mean multiple PHP opening & closing tags.

Like in cases you want to echo a large block of HTML markup, it is always better to close the php tag ?> and then start it again when required. For example:

<?php
if($ok) {
    ?> A large
    block
    HTML MarkUp text
 <?php
} //End of if($ok)
?>

It actually improves the speed as the control will not parse all of the above large block and simply skip to the next < ? php tag or If end point, as compared to echoing all these lines.

查看更多
在下西门庆
3楼-- · 2019-01-20 09:28

You can read about this at documentation.

查看更多
Luminary・发光体
4楼-- · 2019-01-20 09:28

If you're using it embedded in html pages (and I think you are if you have multple PHP blocks), it is normal and I don't think there's a way to avoid it.

If you have an external script, there's no reason to have multiple blocks. Although I don't think it will impact performance.

查看更多
beautiful°
5楼-- · 2019-01-20 09:40

No, it's not causing any slowness or problems. It's perfectly common to have dozens or hundreds of separate <?php ?> blocks in templates.

查看更多
Evening l夕情丶
6楼-- · 2019-01-20 09:43

no, it doesn't do any harm but if you are doing this for example:

<div><?php echo $x; ?></div>

You can use short tags (if it is enabled in your php configuration)

<div><? echo $x; ?></div>

If you want to try and reduce the number of php tags then you could even echo your html like so:

echo '<div>'.$x.'</div>';
查看更多
登录 后发表回答