By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

ECHO or concatenation with single quote, for performance?

P: n/a
Hi,

I am working on an HTML template which has a lot of html tags, with
PHP data shown in the middle of these tags -- you know, the usual.
Currently, I have HTML as is, and many many "echo $variable"
statements mixed in as PHP code.

My question: should I leave it like this, with ECHO statements
embedded within the tags,

Or

Should I concatenate the HTML within single quotes (single quotes are
supposed to be faster in PHP than double quotes) and the display data
as variables, and then ECHO that one concatenated string?

Any experiences or pointers would be great!

Thanks
LB

May 30 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
..oO(L. Berger)
>I am working on an HTML template which has a lot of html tags, with
PHP data shown in the middle of these tags -- you know, the usual.
Currently, I have HTML as is, and many many "echo $variable"
statements mixed in as PHP code.

My question: should I leave it like this, with ECHO statements
embedded within the tags,

Or

Should I concatenate the HTML within single quotes (single quotes are
supposed to be faster in PHP than double quotes) and the display data
as variables, and then ECHO that one concatenated string?
The fastest would be something like

echo 'some text ', $aVar, ' some more text ', $anotherVar;

No double-quoted string, no concatenation, just pure output. But I don't
consider that much of an issue. You should use what seems to be the most
appropriate to you, mainly in order to keep the code readable and
maintainable. For example instead of the code above I would usually use

print "some text $aVar some more text $anotherVar";

or even

printf('some text %s some more text %s',
$aVar,
$anotherVar
);

Definitely slower than the echo statement, but much more flexible and
more readable (at least for me), especially when there are a lot of
variables or statements required in the string.

Micha
May 30 '07 #2

P: n/a
The fastest would be something like
echo 'some text ', $aVar, ' some more text ', $anotherVar;

Thanks, I had no idea echo could work with simple commas! Some testing
with microtime (just back of the envelope, nothing scientific) reveals
that it is indeed the fastest. Here's what I had:

OPTION 1: echo 'something and more' . $i . $i+100 . 'something else
and then some';
OPTION 2: echo 'something and more' , $i , $i+100 , 'something else
and then some';
OPTION 3: something and more' <?php echo $i; ?><?php echo $i+100; ?
>something else and then some
Option 2 outperforms the others. 1 and 2 are almost the same, but 2 is
usually just a little bit faster, especially for more variables.
Option 3, which calls echo many times is markedly slower.

Hope this helps someone!

May 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.