Forum Moderators: coopster

Message Too Old, No Replies

print/echo statements versus html outside the code

Which is better with regards to cpu usage?

         

Captaffy

6:10 am on Jun 22, 2003 (gmt 0)

10+ Year Member



Now this is probably an anal retentive sort of question, but when the parser leaves the <?php?> brackets to output text, is there a cpu hit of any non-negligable size? What if it's going in and out of code repeatedly?

jatar_k

2:55 pm on Jun 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



good question

In my experience I haven't noticed any detriment to jumping in and out of the parser. If memory serves i believe andreasfrierich benchmarked a while back and was quite surprised to find that it wasn't a hit at all. I believe it made only the slightest of differences.

dvduval

3:10 pm on Jun 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As a follow up, I hope I might ask, how do you determine whether to use print or echo?

jatar_k

3:57 pm on Jun 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well I mostly use echo.

I don't think there is much of a difference really. Again andreas benchmarked it and we found very little difference.

One thing with echo was that you should use commas between values and it saves time.

echo "<p>" . $somevar . "<br>&nbsp;";

should be
echo "<p>",$somevar,"<br>&nbsp;";

then php doesn't have to construct the string before it echo, it can just spit out the bits as it goes.

Afkamm

9:39 am on Jun 23, 2003 (gmt 0)

10+ Year Member



So the following isn't a good way of doing it,

echo "<a href=\"www.domain.com\">$value1</a>";

and in actual fact I should be doing,

echo "<a href=\"www.domain.com\">",$value1,"</a>";

instead? Thanks.

Marc :-)

jatar_k

4:23 pm on Jun 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



actually the first one is fine in that case. It is one string, it is only when you need to use the concatenation operator that you should use commas instead.

$somearr[$counter][0] won't be resolved between double quotes so you need to do it the other way.

echo "<a href=\"www.domain.com\">",$somearr[$counter][0],"</a>";

with a normal var you can just use the first example you had.

RonPK

5:05 pm on Jun 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo "<a href=\"www.domain.com\">",$somearr[$counter][0],"</a>";

I remember reading about the 'overhead' caused by escaping quotes in strings. Maybe

echo '<a href="www.domain.com">',$somearr[$counter][0],"</a>";

would be even faster?

Afkamm

5:42 pm on Jun 23, 2003 (gmt 0)

10+ Year Member



Or just,
?>
<a href="www.domain.com"><?=$somearr[$counter][0];?></a>
<?

?

I tend to come out of PHP when I require a moderate amount of HTML as I hate having to slash the quotes :D

Marc :-)

daisho

7:16 pm on Jun 23, 2003 (gmt 0)

10+ Year Member




echo "<a href=\"www.domain.com\">",$somearr[$counter][0],"</a>";

Not that it directly answers the asked question but as another tidbit you could also do this:

echo "<a href=\"www.domain.com\">${somearr[$counter][0]}</a>";

The brace brackets solve the problem.

daisho.

grahamstewart

1:28 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hate having to slash the quotes

I usually use ' quotes to avoid this..


print '<a href="www.domain.com">.$somearr[$counter][0].'</a>';

bonanza

1:42 am on Jun 24, 2003 (gmt 0)



And don't forget the good ol' perlish heredoc:

echo <<< END_TEXT
Put any code in here you want<br>
Including <b>HTML</b>, "quotes", and $variables
and not having to
'worry'
about
\b\a\c\k\s\l\a\s\h\e\s.
END_TEXT;

Very nice for big blocks of html smattered with variables.

grahamstewart

2:11 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm.. true..

I usually avoid using heredoc because it confuses my text editors syntax highlighting. But thats pretty useful.

Anyone got a syntax definition file for TextPad that can cope properly with Heredoc and HTML?

jatar_k

3:00 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



i have the php4 and html syntax and it works fairly well. The killer is as soon as you use a > the syntax highlighting kicks out.

Most of my preproduction stuff ends up with?><? running around to kick the syntax highlighting back on. I forget to remove them every once in a while, raises some questions. ;)

It is always hilarious how many ways we have to do some very simple things. Makes you wonder sometimes if best practice exists or it is just best practice for you.