What is the best way to add two strings together?

前端 未结 12 508
自闭症患者
自闭症患者 2020-12-01 23:35

I read somewehere (I thought on codinghorror) that it is bad practice to add strings together as if they are numbers, since like numbers, strings cannot be changed. Thus, ad

12条回答
  •  心在旅途
    2020-12-02 00:10

    The advice you have read may have been related to the echo function, for which it's quicker to use commas, eg:

    echo $str1, $str2;
    

    Another approach is to build up a string in a variable (eg using the . operator) then echo the whole string at the end.

    You could test this yourself using the microtime function (you'll need to make a loop that repeats eg 1,000 or 100,000 times to make the numbers significant). But of the four you posted, the first one is likely to be the fastest. It's also the most readable - the others don't really make sense programmatically.

提交回复
热议问题