Multiple PHP tags

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 :)


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

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:

if($ok) {
    ?> A large
    HTML MarkUp text
} //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.

You can read about this at documentation.

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.

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>';

Need Your Help

Easy way to make vector triplet in CPP

c++ vector

What would be an easy way to construct a vector triplet of ints in CPP?