+5  A: 

Don't think, profile.

Run scripts that use each version of the function for 100,000 repetitions under, say, the Unix time command. Do not philosophize about what is faster; find out.

chaos
Profiling is, of course, always "the right" answer, but thinking is never a bad idea. Understanding what can and can't effect performance and what's actually going on with your code will lead to you to programming things better the first time.
Alan Storm
"Don't think" is, of course, a bit hyperbolic for the sake of emphasizing a point.
chaos
A: 

[...] so little time optimizations count.

No, they don't.

The only true optimisation is the one that helps YOU read and/or write code faster. You should not sacrifice simplicity or readibility for performance - it will slow you down in long run.

Passing things by reference can be especially misleading. You may run into weird problems later, when some var will change mysteriously. You modify function input, which is not the way things normally work. Every time you do thing the unusual way you have to remember about them. Your memory and attention is limited though. Computer's is not. Don't overoptimise.

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. --Brian W. Kernighan

UPDATE

In this case your whole function escapeCSV is slightly pointless. You should use fputcsv, which is core PHP library written in C, thus is by far faster and memory efficient.

Michał Rudnicki
Downvote wasn't me, and I agree that you should write readable code. However, if your site runs too slow you'll lose visitors (customers?) because they won't want to wait for your page to load. Thus execution performance can be important!
PTBNL
Not in this case. You're looking for microsecond optimisations. This is just poitless.
Michał Rudnicki
Yes, I found out later about fputcsv. But anyway, What you wrote before is ok, I totally agree, for large projects.But this is just a file importing script, the function is declared inside the script and won't be used by any other programmer.
Petruza
+1  A: 

My guess would be that this is a hardly noticeable optimization, that could potentially lead to unexpected sideeffects down the road. For example, the next guy is probably NOT going to expect escapeCSV to modify his parameter, and he might use the parameter he passed in thinking it is still the un-CSV'd string.

Also, since strings aren't edited in-place, you've already got two cases where a new string is created in your function (one for str_replace, one for the concatenation of quotes before/after it). One more for the pass-by-value case isn't going to be the bottleneck in your app.

Kip
+1  A: 

PHP references are not pointers. They do not speed anything up - In fact they do the contrary, since they require an additional internal entry in the symbol table.

troelskn