PHP String convention

PHP String convention

am 28.10.2009 17:15:18 von Nick Cooper

Hi,

I was just wondering what the difference/advantage of these two
methods of writing a string are:

1) $string = "foo{$bar}";

2) $string = 'foo'.$bar;

I always use method 2 but have been noticing method 1 more and more in
source code. Is this just user preference?

I would use a generic search engine but not sure what the first method
is called so don't know where to begin my search.

Thanks for any help.

Nick

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: PHP String convention

am 28.10.2009 17:18:18 von Jay Blanchard

[snip]I was just wondering what the difference/advantage of these two
methods of writing a string are:[/snip]

Method 2 is faster, YMMV.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP String convention

am 28.10.2009 17:20:12 von List Manager

Nick Cooper wrote:
> Hi,
>
> I was just wondering what the difference/advantage of these two
> methods of writing a string are:
>
> 1) $string = "foo{$bar}";
>
> 2) $string = 'foo'.$bar;
>
> I always use method 2 but have been noticing method 1 more and more in
> source code. Is this just user preference?
>
> I would use a generic search engine but not sure what the first method
> is called so don't know where to begin my search.
>
> Thanks for any help.
>
> Nick
>

I think it is a matter of personal preference. I prefer method 1 myself.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP String convention

am 28.10.2009 17:29:27 von Nick Cooper

2009/10/28 Jim Lucas:
> Nick Cooper wrote:
>> Hi,
>>
>> I was just wondering what the difference/advantage of these two
>> methods of writing a string are:
>>
>> 1) $string =3D "foo{$bar}";
>>
>> 2) $string =3D 'foo'.$bar;
>>
>> I always use method 2 but have been noticing method 1 more and more in
>> source code. Is this just user preference?
>>
>> I would use a generic search engine but not sure what the first method
>> is called so don't know where to begin my search.
>>
>> Thanks for any help.
>>
>> Nick
>>
>
> I think it is a matter of personal preference. =A0I prefer method 1 mysel=
f.
>
>
>

Thank you for the quick replies. I thought method 2 must be faster
because it doesn't have to search for variables in the string.

So what is the advantages then of method 1 over 3, do the curly braces
mean anything?

1) $string =3D "foo{$bar}";

2) $string =3D 'foo'.$bar;

3) $string =3D "foo$bar";

I must admit reading method 1 is easier, but writing method 2 is
quicker, is that the only purpose the curly braces serve?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP String convention

am 28.10.2009 17:33:56 von Ashley Sheridan

--=-BZYK76aZIm0krmfIT+kC
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Wed, 2009-10-28 at 16:29 +0000, Nick Cooper wrote:

> 2009/10/28 Jim Lucas:
> > Nick Cooper wrote:
> >> Hi,
> >>
> >> I was just wondering what the difference/advantage of these two
> >> methods of writing a string are:
> >>
> >> 1) $string = "foo{$bar}";
> >>
> >> 2) $string = 'foo'.$bar;
> >>
> >> I always use method 2 but have been noticing method 1 more and more in
> >> source code. Is this just user preference?
> >>
> >> I would use a generic search engine but not sure what the first method
> >> is called so don't know where to begin my search.
> >>
> >> Thanks for any help.
> >>
> >> Nick
> >>
> >
> > I think it is a matter of personal preference. I prefer method 1 myself.
> >
> >
> >
>
> Thank you for the quick replies. I thought method 2 must be faster
> because it doesn't have to search for variables in the string.
>
> So what is the advantages then of method 1 over 3, do the curly braces
> mean anything?
>
> 1) $string = "foo{$bar}";
>
> 2) $string = 'foo'.$bar;
>
> 3) $string = "foo$bar";
>
> I must admit reading method 1 is easier, but writing method 2 is
> quicker, is that the only purpose the curly braces serve?
>


This was on the list a few days back. Basically, the braces are there to
force PHP to recognise the full variable name, so that you could type:

$string = "{$foo}bar";

$string = "foo{$bar[0][1]}";

$string = "{$foo->bar}";


Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-BZYK76aZIm0krmfIT+kC--

Re: PHP String convention

am 28.10.2009 17:36:23 von List Manager

Nick Cooper wrote:
> 2009/10/28 Jim Lucas:
>> Nick Cooper wrote:
>>> Hi,
>>>
>>> I was just wondering what the difference/advantage of these two
>>> methods of writing a string are:
>>>
>>> 1) $string = "foo{$bar}";
>>>
>>> 2) $string = 'foo'.$bar;
>>>
>>> I always use method 2 but have been noticing method 1 more and more in
>>> source code. Is this just user preference?
>>>
>>> I would use a generic search engine but not sure what the first method
>>> is called so don't know where to begin my search.
>>>
>>> Thanks for any help.
>>>
>>> Nick
>>>
>> I think it is a matter of personal preference. I prefer method 1 myself.
>>
>>
>>
>
> Thank you for the quick replies. I thought method 2 must be faster
> because it doesn't have to search for variables in the string.
>
> So what is the advantages then of method 1 over 3, do the curly braces
> mean anything?
>
> 1) $string = "foo{$bar}";
>
> 2) $string = 'foo'.$bar;
>
> 3) $string = "foo$bar";
>
> I must admit reading method 1 is easier, but writing method 2 is
> quicker, is that the only purpose the curly braces serve?
>

They tell PHP to view the text between the curly braces as a variable that needs
interpreting.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP String convention

am 28.10.2009 18:18:17 von Kim Madsen

Hi Nick

Nick Cooper wrote on 2009-10-28 17:29:

> Thank you for the quick replies. I thought method 2 must be faster
> because it doesn't have to search for variables in the string.
>
> So what is the advantages then of method 1 over 3, do the curly braces
> mean anything?
>
> 1) $string = "foo{$bar}";
>
> 2) $string = 'foo'.$bar;
>
> 3) $string = "foo$bar";
>
> I must admit reading method 1 is easier, but writing method 2 is
> quicker, is that the only purpose the curly braces serve?

Yes, you're right about that. 10 years ago I went to a seminar were
Rasmus Lerforf was speaking and asked him exactly that question. The
single qoutes are preferred and are way faster because it doesn´t have
to parse the string, only the glued variables.

Also we discussed that if you´re doing a bunch of HTML code it's
considerably faster to do:





Than
print "
\n\t
\n\t\t$data
\n\t";

or
print '

'.$data.'
';

I remember benchmark testing it afterwards back then and there was
clearly a difference.

--
Kind regards
Kim Emax - masterminds.dk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: PHP String convention

am 28.10.2009 18:36:43 von Warren Vail

The curly braces look like something from the smarty template engine.

Warren Vail
-----Original Message-----
From: Kim Madsen [mailto:php.net@emax.dk]=20
Sent: Wednesday, October 28, 2009 10:18 AM
To: Nick Cooper
Cc: Jim Lucas; php-general@lists.php.net
Subject: Re: [PHP] PHP String convention

Hi Nick

Nick Cooper wrote on 2009-10-28 17:29:

> Thank you for the quick replies. I thought method 2 must be faster
> because it doesn't have to search for variables in the string.
>=20
> So what is the advantages then of method 1 over 3, do the curly braces
> mean anything?
>=20
> 1) $string =3D "foo{$bar}";
>=20
> 2) $string =3D 'foo'.$bar;
>=20
> 3) $string =3D "foo$bar";
>=20
> I must admit reading method 1 is easier, but writing method 2 is
> quicker, is that the only purpose the curly braces serve?

Yes, you're right about that. 10 years ago I went to a seminar were=20
Rasmus Lerforf was speaking and asked him exactly that question. The=20
single qoutes are preferred and are way faster because it doesn=B4t have =

to parse the string, only the glued variables.

Also we discussed that if you=B4re doing a bunch of HTML code it's=20
considerably faster to do:





Than
print "
\n\t
\n\t\t$data
\n\t";

or
print '

'.$data.'
';

I remember benchmark testing it afterwards back then and there was=20
clearly a difference.

--=20
Kind regards
Kim Emax - masterminds.dk

--=20
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP String convention

am 28.10.2009 18:39:52 von Ashley Sheridan

On Wed, 2009-10-28 at 18:18 +0100, Kim Madsen wrote:
> Hi Nick
>=20
> Nick Cooper wrote on 2009-10-28 17:29:
>=20
> > Thank you for the quick replies. I thought method 2 must be faster
> > because it doesn't have to search for variables in the string.
> >=20
> > So what is the advantages then of method 1 over 3, do the curly braces
> > mean anything?
> >=20
> > 1) $string =3D "foo{$bar}";
> >=20
> > 2) $string =3D 'foo'.$bar;
> >=20
> > 3) $string =3D "foo$bar";
> >=20
> > I must admit reading method 1 is easier, but writing method 2 is
> > quicker, is that the only purpose the curly braces serve?
>=20
> Yes, you're right about that. 10 years ago I went to a seminar were=20
> Rasmus Lerforf was speaking and asked him exactly that question. The=20
> single qoutes are preferred and are way faster because it doesn´t ha=
ve=20
> to parse the string, only the glued variables.
>=20
> Also we discussed that if you´re doing a bunch of HTML code it's=20
> considerably faster to do:
>=20
>
>
>
>=20
> Than
> print "
> \n\t
> \n\t\t$data
> \n\t";
>=20
> or
> print '
>
> '.$data.'
> ';
>=20
> I remember benchmark testing it afterwards back then and there was=20
> clearly a difference.
>=20
> --=20
> Kind regards
> Kim Emax - masterminds.dk
>=20

Or, far easier still to do:

print << =20
$data

=20
$data

EOC;

than:

=20


=20



Also, the use of short tags in the second example will almost certainly cau=
se problems later on if you want to do anything with XML output from PHP.

Thanks,
Ash
http://www.ashleysheridan.co.uk




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP String convention

am 28.10.2009 19:06:55 von Robert Cummings

Kim Madsen wrote:
> Hi Nick
>
> Nick Cooper wrote on 2009-10-28 17:29:
>
>> Thank you for the quick replies. I thought method 2 must be faster
>> because it doesn't have to search for variables in the string.
>>
>> So what is the advantages then of method 1 over 3, do the curly braces
>> mean anything?
>>
>> 1) $string = "foo{$bar}";
>>
>> 2) $string = 'foo'.$bar;
>>
>> 3) $string = "foo$bar";
>>
>> I must admit reading method 1 is easier, but writing method 2 is
>> quicker, is that the only purpose the curly braces serve?
>
> Yes, you're right about that. 10 years ago I went to a seminar were
> Rasmus Lerforf was speaking and asked him exactly that question. The
> single qoutes are preferred and are way faster because it doesn´t have
> to parse the string, only the glued variables.
>
> Also we discussed that if you´re doing a bunch of HTML code it's
> considerably faster to do:
>
>
>
>
>
> Than
> print "
> \n\t
> \n\t\t$data
> \n\t";
>
> or
> print '
>
> '.$data.'
> ';
>
> I remember benchmark testing it afterwards back then and there was
> clearly a difference.

10 years is a long time... there have been benchmarks posted to this
list in the past year or so indicating that in the PHP5 release there is
no real difference in speed between the use of single or double quotes.
If I recall correctly double quotes may even be eking out a small
advantage over single quotes nowadays.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP String convention

am 04.11.2009 15:50:49 von Nathan Rixham

Nick Cooper wrote:
> Hi,
>
> I was just wondering what the difference/advantage of these two
> methods of writing a string are:
>
> 1) $string = "foo{$bar}";
>
> 2) $string = 'foo'.$bar;

1) breaks PHPUnit when used in classes (need to bug report that)
2) [concatenation] is faster (but you wouldn't notice)

comes down to personal preference and what looks best in your (teams)
IDE I guess; legibility (and possibly portability) is probably the
primary concern.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: PHP String convention

am 04.11.2009 16:16:54 von Lars Torben Wilson

2009/10/28 Warren Vail :
> The curly braces look like something from the smarty template engine.
>
> Warren Vail

Odd. I always thought the curly braces in the Smarty engine looked
like something from PHP. :)


Torben

> -----Original Message-----
> From: Kim Madsen [mailto:php.net@emax.dk]
> Sent: Wednesday, October 28, 2009 10:18 AM
> To: Nick Cooper
> Cc: Jim Lucas; php-general@lists.php.net
> Subject: Re: [PHP] PHP String convention
>
> Hi Nick
>
> Nick Cooper wrote on 2009-10-28 17:29:
>
>> Thank you for the quick replies. I thought method 2 must be faster
>> because it doesn't have to search for variables in the string.
>>
>> So what is the advantages then of method 1 over 3, do the curly braces
>> mean anything?
>>
>> 1) $string =3D "foo{$bar}";
>>
>> 2) $string =3D 'foo'.$bar;
>>
>> 3) $string =3D "foo$bar";
>>
>> I must admit reading method 1 is easier, but writing method 2 is
>> quicker, is that the only purpose the curly braces serve?
>
> Yes, you're right about that. 10 years ago I went to a seminar were
> Rasmus Lerforf was speaking and asked him exactly that question. The
> single qoutes are preferred and are way faster because it doesn=B4t have
> to parse the string, only the glued variables.
>
> Also we discussed that if you=B4re doing a bunch of HTML code it's
> considerably faster to do:
>
>
> =A0
>
>
> Than
> print "
> \n\t
> =A0 \n\t\t$data
> \n\t";
>
> or
> print '
>
> =A0 '.$data.'
> ';
>
> I remember benchmark testing it afterwards back then and there was
> clearly a difference.
>
> --
> Kind regards
> Kim Emax - masterminds.dk
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: PHP String convention

am 04.11.2009 16:27:00 von Lars Torben Wilson

2009/11/4 Nathan Rixham :
> Nick Cooper wrote:
>>
>> Hi,
>>
>> I was just wondering what the difference/advantage of these two
>> methods of writing a string are:
>>
>> 1) $string = "foo{$bar}";
>>
>> 2) $string = 'foo'.$bar;
>
> 1) breaks PHPUnit when used in classes (need to bug report that)
> 2) [concatenation] is faster (but you wouldn't notice)
>
> comes down to personal preference and what looks best in your (teams) IDE I
> guess; legibility (and possibly portability) is probably the primary
> concern.

I would tend to agree here; the concat is faster but you may well only
notice in very tight loops. The curly brace syntax can increase code
readability, depending on the complexity of the expression. I use
them both depending on the situation.

Remember the rules of optimization:

1) Don't.
2) (Advanced users only): Optimize later.

Write code so that it's readable, and then once it's working, identify
the bottlenecks and optimize where needed. If you understand code
analysis and big-O etc then you will start to automatically write
mostly-optimized code anyway and in general, I doubt that you'll often
identify the use of double quotes as a bottleneck--it almost always
turns out that other operations and code structures are far more
expensive and impact code speed much more.

That said, you don't really lose anything by using concatenation from
the start, except perhaps some legibility, so as Nathan said it often
really just comes down to personal preference and perhaps the house
coding conventions.


Regards,

Torben

> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php