0

In the absence of conditional statements, consider named variables x, y and z, defined as basic string variables--pulled from a query or whatnot. In a PHP file, reading such back in a web page could be like this:

<?php
$x= blah, blah, blah;
$y= blah, blah, blah;
$z= blah, blah, blah;

echo "<p>$x</p>/n<p>$y</p>/n</p>;
?>

(of course) Similarly, in an ASP (VBScript) file), such wou go like:

<%

x= blah, blah, blah
y= blah, blah, blah
z= blah, blah, blah
Response.Write "<p>" & x & "</p>" & vbcrlf & "<p>" & y & "</p> & vbcrlf & "<p>" & z & "</p>"
%>

Alternatively, such could be written as: (php example)

<p><?php echo $x;?></p>
<p><?php echo $y;?></p>
<p><?php echo $z;?></p>

(asp example)

<p><% Response.Write x %></p>
<p><% Response.Write y %></p>
<p><% Response.Write z %></p>

I read somewhere that in as *.asp file, switching back and forth between execution mode and static html mode slows the server down, hence should be avoided. For quite a while, I have wondered if such holds true ion php. From what I gather, there is little or no difference with php. It has been suggested that the reverse might be true. That is, in the first above example, the php parser must "interpret" static html when such is in an echo statement.

Any ideas?

mr. t
  • 1

1 Answers1

0

PHP file can have mixed content with code within tags embedded in a HTML document. Code outside tags is ignored by the parser, leaving it to be interpreted by client browser.Every time opening PHP tag is encountered, parser starts rendering the output to the client until closing tag is reached. If code consists of conditional statement, the parser determines which block to be skipped.Again till another opening tag comes, everything is treated as HTML leaving the browser to process the same.

If you use php, to execute it, this will be using your servers resources and be computing. Definitely if you run plain html it will be processed faster as compared to mixed contents but PHP text processing is also faster you will not see any difference.

But it is efficient if you follow some guidelines,

@ For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo or print.

@ Never echo static text or HTML. Leave that outside of your PHP

https://www.php.net/manual/en/language.basic-syntax.phpmode.php https://softwareengineering.stackexchange.com/questions/180501/which-is-better-to-include-html-inside-php-code-or-outside-it

SK IRT
  • 127
  • 1
  • 11
  • To be sure “Never echo static text or HTML” raises some questions: (1) particularly with PHP, the $ character allows for the possibility of reducing sheer file size. It is shorter to write “

    $x

    ” than to write “

    .” How much actual interpretation is in question, as function names would be set apart by breaking out of static text/HTML with either a single or double quote, as in “ "text text text" . Function . "more text".” Also, if interpretation is a issue in PHP, would such not be an issue in other platforms? For example, in an *asp file, " becomes "" instead of \"
    – mr. t Apr 04 '21 at 17:25
  • You can use PHP short tags for saving time and size concern “

    =$x;?>

    .” As i already said never echo static text. All your logic should go in PHP function/class/module and call it wherever necessary. Sorry but i didn't get your second part of your comment regarding function.
    – SK IRT Apr 06 '21 at 03:28
  • This is in contrast with the other said platform, in which the recommendation is DO “echo” static text/HTML instead of switching back and forth between execution mode and static text/HTML mode. That is, “echo” becomes “Response.Write.” In this case, a literal double quotation mark within a said text string is escaped by making it a double double quotation mark ("") instead of preceding the same with a reverse slash mark. Hence “echo "\"" . $x . "\""; translates to “Response.Write """" & x & """" . Looks like in either case, there is some interpretation regarding " in echo or Response.Write. – mr. t Apr 11 '21 at 18:18
  • For what it's worth, having first read about the recommendation for *.asp files, I later figured the same rationale would apply to *.php files. In practice, I never could tell any difference--I assume such is generally not in excess of a few milliseconds. – mr. t Apr 11 '21 at 18:27
  • Ok..i am basically PHP guy but i have searched and found that in ASP we can use Response.buffer option for better performance in case of switching between static and execution mode. Reference: Point 14 and 15 from https://learn.microsoft.com/en-us/previous-versions/office/developer/server-technologies/ms972335(v=msdn.10)?redirectedfrom=MSDN#asptips_topic15 .Check out this link as well https://stackoverflow.com/questions/151448/response-write-vs – SK IRT Apr 13 '21 at 04:34