473,698 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4067
..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
3638
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
4709
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, 20-30k), what are the performance differences (if any with something as trivial as this) between initializing a new instance of StringBuilder with a specified capacity vs. initializing a new instance without... (the final length is not fixed) ie,
9
2055
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
6244
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
4672
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 of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
34
2648
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. ____________ THE OVERVIEW I don't remember where I picked it up, but I remember reading years ago that the simple, obvious Python approach for string concatenation: x = "a" + "b"
11
1854
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. echo "error is $error";
34
3538
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 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical difference?
32
2647
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 use a php session cookie to stop the repeating of an embedded video file on a per session basis; i amended the php code to display a still pic if the session cookie value
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.