2009/11/4 Nathan Rixham <nrix...@gmail.com>:
> 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

Reply via email to