开发者

What's the fastest way of concatenating strings in PHP?

开发者 https://www.devze.com 2023-01-15 16:59 出处:网络
I want to combine two variables together: $var1 = \'Welcome \'; $var2 = $_SESSION[\'UserName\']; Which of these will work faster? Code sample 1:

I want to combine two variables together:

$var1 = 'Welcome ';

$var2 = $_SESSION['UserName'];

Which of these will work faster? Code sample 1:

$var3 = $var1.$var2;

Or code sample 2:

$var3 = "$v开发者_开发问答ar1$var2";


Code sample 1 won't work at all..

Syntax considerations set aside, Sample 1 should be trivially faster because it doesn't involve parsing a string (looking for variables).

But it's very, very trivial..


Both examples would provide the same result - $var3 equal to "Welcome Wazzy". However, Code Sample 1 will work significantly faster. Try it out with large sets of data (or via concatenating small sets a million times or so), and you will see that concatenation works significantly faster than variable substitution.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号