473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ECHO or concatenation with single quote, for performance?

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
2 4046
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
9
by: windandwaves | last post by:
Hi Folk My question is: echo all the time vs echo at the end - what is faster Let me explain I used to write pages like this: echo "<head> ";
23
by: Bonj | last post by:
what is the correct form of string concatenation in VB.NET, + or &? Both seem to work, but which is correct? I know it's + in C# and & in VB6, but which in VB.NET?
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
11
by: Twayne | last post by:
Hi, Irrelevant question time, probably: Is there any advantage/reason to use one format over the other for the following two types of echo statements? 1. echo "error is " . $error; 2. ...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
32
by: Request-1 | last post by:
hi folks, html coder here, new and terrified * of php!! aaaaaa! i'm trying to bury a JS script to rotate a photo, in a page i converted from html to php. the conversion went well, it was to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.