Re: concat vs variable in string



On Aug 13, 11:58 pm, Mark <mnbaya...@xxxxxxxxx> 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)

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.
.



Relevant Pages

  • Re: string concatenation optimizations [from python-dev Summary]
    ... Essentially what I have in mind is a type that's a list of byte ... The value is defined as the concatenation of these arrays. ... > way to concatenate a bunch of strings. ...
    (comp.lang.python)
  • Re: why use the data command parameter collection
    ... cumbersome than using an iterative object oriented approach. ... quote characters or other such input by concatenating strings. ... Parameters Collection. ... Also the approach string concatenation leaves a lot to be desired (see ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: deepening into fortran 90,95, 2003
    ... I've been bitten many times by the slowness of concatenation of strings. ... Support the Original G95 Project: http://www.g95.org ... Support the GNU GFortran Project: http://gcc.gnu.org/fortran/index.html ...
    (comp.lang.fortran)
  • Re: From D
    ... string concatenation and allow numeric literals to implicitly concatenate? ... Did you miss the bit where Python ALREADY does this for strings? ... because two int tokens can be "concatenated" to make a single int token, ...
    (comp.lang.python)
  • Re: handling single quotes??
    ... It will be seen as two strings next to each ... other without any concatenation. ... > At least with SQL Server, it recognizes two single quotes as a single ... > Notice two single quotes between b and s of Bob's. ...
    (microsoft.public.dotnet.framework.aspnet)