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

concat vs variable in string

P: n/a
Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?

Or does it still do a concatenation behind the scenes? I hear
concatenation is expensive... (even if its negligible in this scenario)
Aug 13 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a

"Mark" <mn*******@gmail.comha scritto nel messaggio
news:a4**********************************@o40g2000 prn.googlegroups.com...
Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?

Or does it still do a concatenation behind the scenes? I hear
concatenation is expensive... (even if its negligible in this scenario)

$_SERVER['DOCUMENT_ROOT'] . '/inc/mysql-connect.php'

is the most appropriate method because php should not parse everything
within the string.
The normal concatenation theorically should be more efficient with the use
of the apostrophes
that don't support (afaik) the overhead of the parsing of the variables
within the strings (and so on).
Try to avoid the inclusion of the variables within the strings, it's faster
and you (and someone else that can read)
can identify clearly the variables within the source code.
Regards

L. A. Iarrusso
Aug 14 '08 #2

P: n/a
Mark wrote:
Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?

Or does it still do a concatenation behind the scenes? I hear
concatenation is expensive... (even if its negligible in this scenario)
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php" should be slightly
more efficient. But in practice, your example will have virtually no
difference.

If you're that close to maximizing your server, you need a faster server
anyway.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 14 '08 #3

P: n/a
On Aug 13, 8:35*pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Mark wrote:
Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?
Or does it still do a concatenation behind the scenes? I hear
concatenation is expensive... (even if its negligible in this scenario)

$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php" should be slightly
more efficient. *But in practice, your example will have virtually no
difference.

If you're that close to maximizing your server, you need a faster server
anyway.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================
Haha.. I was thinking more about the scenario where I need to output
some variables in a loop :p Thanks for the tips guys :)
Aug 14 '08 #4

P: n/a
Mark wrote:
Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?
Thats a bad question but see here:

$var = 'Im a test string';
for ($i = 0; $i < 100000; ++$i)
{
#$tmp = $var . "..."; # 0.061450 sec
#$tmp = $var . '...'; # 0.061190 sec
#$tmp = "{$var}..."; # 0.074712 sec

#$tmp = $var . "..." . $var . "..."; # 0.091580 sec
#$tmp = $var . '...' . $var . '...'; # 0.090936 sec
#$tmp = "{$var}...{$var}..."; # 0.092433

#$tmp = $var."...".$var."...".$var."...".$var."...".$var." ...";
# 0.185287
#$tmp = $var.'...'.$var.'...'.$var.'...'.$var.'...'.$var.' ...';
# 0.182607
#$tmp = "{$var}...{$var}...{$var}...{$var}...{$var}... ";
# 0.154140
}

the executing time after each variation code comes from zend-profiler.

You can see, there is no unique answer for it.

So long, Ulf
Aug 14 '08 #5

P: n/a
J2Be.com wrote:
"Mark" <mn*******@gmail.comha scritto nel messaggio
news:a4**********************************@o40g2000 prn.googlegroups.com...
>Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?

Or does it still do a concatenation behind the scenes? I hear
concatenation is expensive... (even if its negligible in this scenario)


$_SERVER['DOCUMENT_ROOT'] . '/inc/mysql-connect.php'

is the most appropriate method because php should not parse everything
within the string.
The normal concatenation theorically should be more efficient with the use
of the apostrophes
that don't support (afaik) the overhead of the parsing of the variables
within the strings (and so on).
Try to avoid the inclusion of the variables within the strings, it's faster
and you (and someone else that can read)
can identify clearly the variables within the source code.
Regards

L. A. Iarrusso

I think that's a matter of opinion. I for one find putting variables
in strings easier to read than multitudes of periods and escape slashes
many people use.

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Aug 14 '08 #6

P: n/a
Ulf Kadner wrote:
Mark wrote:
>Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?

Thats a bad question but see here:-*/9
But an interesting answer.

I've wondered about the penalty for the interpreted "" over '', as I
see double quotes commonly used for strings that contain no variables. I
always thought that was not good practice, but there is little penalty.

Are heredocs cheap to run? I rarely see heredocs in others php code.
I suspect that's more a style issue. Coming from a perl background
influences my coding styles, php seems wordy!

Jeff
>
$var = 'Im a test string';
for ($i = 0; $i < 100000; ++$i)
{
#$tmp = $var . "..."; # 0.061450 sec
#$tmp = $var . '...'; # 0.061190 sec
#$tmp = "{$var}..."; # 0.074712 sec

#$tmp = $var . "..." . $var . "..."; # 0.091580 sec
#$tmp = $var . '...' . $var . '...'; # 0.090936 sec
#$tmp = "{$var}...{$var}..."; # 0.092433

#$tmp = $var."...".$var."...".$var."...".$var."...".$var." ...";
# 0.185287
#$tmp = $var.'...'.$var.'...'.$var.'...'.$var.'...'.$var.' ...';
# 0.182607
#$tmp = "{$var}...{$var}...{$var}...{$var}...{$var}... ";
# 0.154140
}

the executing time after each variation code comes from zend-profiler.

You can see, there is no unique answer for it.

So long, Ulf
Aug 14 '08 #7

P: n/a
On Aug 13, 11:58 pm, Mark <mnbaya...@gmail.comwrote:
Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?

Or does it still do a concatenation behind the scenes? I hear
concatenation is expensive... (even if its negligible in this scenario)
If you use single quotes instead of double, then PHP won't attempt to
do variable substitution on a string, which is where the real cost
lies. However, I don't think there's a great deal of difference
between var subbing in double quotes and concatting with single quotes
and variables. My understanding is that concatting is slightly
faster, but the differences aren't huge unless you're working with
massive strings or a long loop.

The bigger advantage, as far as I'm concerned, is that with the
variables not inside strings they get syntax highlighted, whereas they
don't when they're embedded in a string. additionally, as PHP is more
often than not outputting HTML and XML, where attributes are expected
to be wrapped in double quoted strings, using single quoted strings
means you usually don't have to rely on escaping quote characters to
get the correct output.
Aug 14 '08 #8

P: n/a
Mark wrote:
On Aug 13, 8:35 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Mark wrote:
>>Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?
Or does it still do a concatenation behind the scenes? I hear
concatenation is expensive... (even if its negligible in this scenario)
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php" should be slightly
more efficient. But in practice, your example will have virtually no
difference.

If you're that close to maximizing your server, you need a faster server
anyway.
Haha.. I was thinking more about the scenario where I need to output
some variables in a loop :p Thanks for the tips guys :)
My statement still stands.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 14 '08 #9

P: n/a
..oO(Mark)
>Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?
Doesn't really matter. Use whatever you prefer.
>Or does it still do a concatenation behind the scenes? I hear
concatenation is expensive...
Google "premature optimization".

Micha
Aug 14 '08 #10

P: n/a
On Aug 14, 10:32*am, Michael Fesser <neti...@gmx.dewrote:
.oO(Mark)
Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?

Doesn't really matter. Use whatever you prefer.
Or does it still do a concatenation behind the scenes? I hear
concatenation is expensive...

Google "premature optimization".

Micha
It was for future reference. Yes, premature optimization is bad, but
it's not like I'm spending hours optimizing an algorithm. It's just a
matter of writing it differently. But then again, I wound up here, so
maybe I am wasting time.
Aug 14 '08 #11

P: n/a
On Aug 14, 1:21*am, Ulf Kadner <dr_lo...@gmx.netwrote:
Mark wrote:
Is
"{$_SERVER['DOCUMENT_ROOT']}/inc/mysql-connect.php"
more efficient than
$_SERVER['DOCUMENT_ROOT']."/inc/mysql-connect.php"
?

Thats a bad question but see here:

$var = 'Im a test string';
for ($i = 0; $i < 100000; ++$i)
{
* * *#$tmp = $var . "..."; * * * * * * * * * * * # 0.061450 sec
* * *#$tmp = $var . '...'; * * * * * * * * * * * # 0.061190 sec
* * *#$tmp = "{$var}..."; * * * * * * * * * ** *# 0.074712 sec

* * *#$tmp = $var . "..." . $var . "..."; * * * *# 0.091580 sec
* * *#$tmp = $var . '...' . $var . '...'; * * * *# 0.090936 sec
* * *#$tmp = "{$var}...{$var}..."; * * * * * * * # 0.092433

* * *#$tmp = $var."...".$var."...".$var."...".$var."...".$var." ....";
* * * * * * * * * * * * * * * * * * * * * * * * *# 0.185287
* * *#$tmp = $var.'...'.$var.'...'.$var.'...'.$var.'...'.$var.' ....';
* * * * * * * * * * * * * * * * * * * * * * * * *# 0.182607
* * *#$tmp = "{$var}...{$var}...{$var}...{$var}...{$var}... ";
* * * * * * * * * * * * * * * * * * * * * * * * *# 0.154140

}

the executing time after each variation code comes from zend-profiler.

You can see, there is no unique answer for it.

So long, Ulf
Those are interesting results... it seems like concatenation with
single quotes is the fastest for short strings, but variables inside
double quotes is faster for longer strings... but we're talking about .
03 seconds for 100K iterations eh? I guess the real answer is just to
write whatever looks cleanest. Thanks :)

Mark
Aug 14 '08 #12

P: n/a
On Thu, 14 Aug 2008 05:55:31 -0700 (PDT), Gordon
<go**********@ntlworld.comwrote in
<e4**********************************@y38g2000hsy. googlegroups.com>:

[snip]
>The bigger advantage, as far as I'm concerned, is that with the
variables not inside strings they get syntax highlighted, whereas they
don't when they're embedded in a string.
That depends on your editor. I've been using the latest version of
Notepad++ recently, and it does highlight embedded variables.

[snip rest]
--
Charles Calvert | Software Design/Development
Celtic Wolf, Inc. | Project Management
http://www.celticwolf.com/ | Technical Writing
(703) 580-0210 | Research
Aug 17 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.