|
View:
New views
13 Messages
—
Rating Filter:
Alert me
|
|
|
PHP String conventionHi,
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[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 conventionNick 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 convention2009/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? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php |
|
|
Re: PHP String conventionOn 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 |
|
|
Re: PHP String conventionNick 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 conventionHi 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: <tr> <td><?= $data ?></td> </tr> Than print " \n\t<tr> \n\t\t<td>$data</td> \n\t</tr>"; or print ' <tr> <td>'.$data.'</td> </tr>'; 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 conventionThe curly braces look like something from the smarty template engine.
Warren Vail -----Original Message----- From: Kim Madsen [mailto:php.net@...] Sent: Wednesday, October 28, 2009 10:18 AM To: Nick Cooper Cc: Jim Lucas; php-general@... 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 = "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: <tr> <td><?= $data ?></td> </tr> Than print " \n\t<tr> \n\t\t<td>$data</td> \n\t</tr>"; or print ' <tr> <td>'.$data.'</td> </tr>'; 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 |
|
|
Re: PHP String conventionOn Wed, 2009-10-28 at 18:18 +0100, 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: > > <tr> > <td><?= $data ?></td> > </tr> > > Than > print " > \n\t<tr> > \n\t\t<td>$data</td> > \n\t</tr>"; > > or > print ' > <tr> > <td>'.$data.'</td> > </tr>'; > > I remember benchmark testing it afterwards back then and there was > clearly a difference. > > -- > Kind regards > Kim Emax - masterminds.dk > Or, far easier still to do: print <<<EOC <tr> <td>$data</td> </tr> <tr> <td>$data</td> </tr> EOC; than: <tr> <td><?= $data ?></td> </tr> <tr> <td><?= $data ?></td> </tr> Also, the use of short tags in the second example will almost certainly cause 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 conventionKim 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: > > <tr> > <td><?= $data ?></td> > </tr> > > Than > print " > \n\t<tr> > \n\t\t<td>$data</td> > \n\t</tr>"; > > or > print ' > <tr> > <td>'.$data.'</td> > </tr>'; > > 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 conventionNick 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 convention2009/10/28 Warren Vail <warren@...>:
> 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@...] > Sent: Wednesday, October 28, 2009 10:18 AM > To: Nick Cooper > Cc: Jim Lucas; php-general@... > 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 = "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: > > <tr> > <td><?= $data ?></td> > </tr> > > Than > print " > \n\t<tr> > \n\t\t<td>$data</td> > \n\t</tr>"; > > or > print ' > <tr> > <td>'.$data.'</td> > </tr>'; > > 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 convention2009/11/4 Nathan Rixham <nrixham@...>:
> 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 |
| Free embeddable forum powered by Nabble | Forum Help |