Re: PHP String convention

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux