Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 898
David Morin Avatar asked Jun 11 '12 11:06

David Morin


People also ask

Can you have multiple PHP in HTML?

Together, you can use Apache and PHP-FPM to host multiple PHP web-applications, each using a different version of PHP, all on the same server, and all at the same time.

How many tags are there in PHP?

There are four different pairs of opening and closing tags which can be used in php.

What is <? PHP tag?

A PHP code script is a text file having . php extension and is stored on web server. The PHP parser on server looks for special sequence of characters <? php and ?>. These are called PHP's opening and closing tags.


2 Answers

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

like image 197
deceze Avatar answered Sep 22 '22 12:09

deceze


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.

like image 32
DavChana Avatar answered Sep 23 '22 12:09

DavChana