PHP {$code} 和 .$code 哪个更快?
PHP what is faster {$code} or .$code?
我不知道如何测试它。
PHP 中哪个更快:
$test = "Text {$code}";
或
$test = "Text ".$code;
?
在现实世界中,您永远不会注意到任何差异,所以不要强调它:)
您不太可能会遇到任何明显的速度问题,但不管怎样,让我解释一下它们之间的差异以及最快的解决方案是什么。
当你使用双引号时 "like this" php 将尝试评估引号内的任何文本,这就是你可以在其中使用 {$code} 的原因,你当然也可能会错过大括号并简单地写 "Text $code" 除非有比正在评估的简单变量更复杂的东西。
我预计你的第二个例子会稍微慢一些(我没有这方面的证据,但我想如果需要的话我们可以为它写一个简单的测试)。由于它首先尝试评估字符串,然后将变量连接到字符串,因此这是一个单独的操作。
如果您真正关心速度,请使用单引号。这些内容永远不会被评估,因此您可以简单地获取文本字符串并连接变量。
像这样:
$test = 'Text ' . $code
你的第一个选项是最快的:
Benchmarks run against PHP 5.2 and 5.3 show that parsing double-quoted
strings with interpolation is no slower (and often faster) than single-
quoted strings using concatenation. When simple strings with no
variables in them are used, the performance is clearly better with
double-quoted strings due to implementation details in the engine.
引用here
我不知道如何测试它。
PHP 中哪个更快:
$test = "Text {$code}";
或
$test = "Text ".$code;
?
在现实世界中,您永远不会注意到任何差异,所以不要强调它:)
您不太可能会遇到任何明显的速度问题,但不管怎样,让我解释一下它们之间的差异以及最快的解决方案是什么。
当你使用双引号时 "like this" php 将尝试评估引号内的任何文本,这就是你可以在其中使用 {$code} 的原因,你当然也可能会错过大括号并简单地写 "Text $code" 除非有比正在评估的简单变量更复杂的东西。
我预计你的第二个例子会稍微慢一些(我没有这方面的证据,但我想如果需要的话我们可以为它写一个简单的测试)。由于它首先尝试评估字符串,然后将变量连接到字符串,因此这是一个单独的操作。
如果您真正关心速度,请使用单引号。这些内容永远不会被评估,因此您可以简单地获取文本字符串并连接变量。
像这样:
$test = 'Text ' . $code
你的第一个选项是最快的:
Benchmarks run against PHP 5.2 and 5.3 show that parsing double-quoted strings with interpolation is no slower (and often faster) than single- quoted strings using concatenation. When simple strings with no variables in them are used, the performance is clearly better with double-quoted strings due to implementation details in the engine.
引用here