PHP字符串连接的差异

PHP字符串连接的差异,php,arrays,concatenation,Php,Arrays,Concatenation,两者之间有区别吗 $newURL = "http://example.com/foo.php?id=" . $_GET["id"]; 及 性能方面?一种编码风格比另一种更好吗?第二种选择似乎不太常见。是因为没有人知道它确实有效,还是因为这种方法有缺点?好吧,第一种方法是字符串串联,而第二种方法是字符串插值 找不到关于它的任何文档,但经过一些测试后,插值似乎稍微快一点。当我说“轻微”时,我的意思是几乎没有区别 我跑了150米,结果如下: 串联:13.905070066452秒 插值:12.48

两者之间有区别吗

$newURL = "http://example.com/foo.php?id=" . $_GET["id"];


性能方面?一种编码风格比另一种更好吗?第二种选择似乎不太常见。是因为没有人知道它确实有效,还是因为这种方法有缺点?

好吧,第一种方法是字符串串联,而第二种方法是字符串插值

找不到关于它的任何文档,但经过一些测试后,插值似乎稍微快一点。当我说“轻微”时,我的意思是几乎没有区别

我跑了150米,结果如下:

  • 串联:13.905070066452秒

  • 插值:12.485554933548秒

以下是我用于测试的代码:

$numberOfExecutions = 150000000; // 150 MILLION!

$ARRAY = array("id" => 1); // to simulate $_GET access

$start = microtime(true);
for($id = 0; $id < $numberOfExecutions; $id++) {
    $newURL = "http://example.com/foo.php?id=" . $ARRAY["id"];
}
$time_elapsed_secs = microtime(true) - $start;
echo $time_elapsed_secs;

echo "</br>";

$start = microtime(true);
for($id = 0; $id < $numberOfExecutions; $id++) {
    $newURL = "http://example.com/foo.php?id=$ARRAY[id]";
}
$time_elapsed_secs = microtime(true) - $start;
echo $time_elapsed_secs;
$numberOfExecutions=15000000;//一亿五千万!
$ARRAY=ARRAY(“id”=>1);//要模拟$\u获取访问权限
$start=microtime(真);
对于($id=0;$id<$numberOfExecutions;$id++){
$newURL=”http://example.com/foo.php?id=“$ARRAY[“id”];
}
$time\u passed\u secs=微时间(true)-$start;
echo$时间\u经过\u秒;
回声“
”; $start=microtime(真); 对于($id=0;$id<$numberOfExecutions;$id++){ $newURL=”http://example.com/foo.php?id=$ARRAY[id]”; } $time\u passed\u secs=微时间(true)-$start; echo$时间\u经过\u秒;
重要:
不过,您还应该看看@Jack在其中看到生成的指令,并得出结论,级联实际上比插值更快。

它们之间的性能没有太大差异。我已经使用AWSEC2 T2微实例对其进行了测试。这里是脚本

conct1.php

<?php
$_GET["id"] = "test";
$i = 0;
$startTime = microtime(true);
while ($i < 1000000) {
    $newURL = "http://example.com/foo.php?id=" . $_GET["id"];
    $i++;
}
$totalTime = microtime(true) - $startTime;
echo "Total Time: " . $totalTime, PHP_EOL;
<?php
$_GET["id"] = "test";
$i = 0;
$startTime = microtime(true);
while ($i < 1000000) {
    $newURL = "http://example.com/foo.php?id=$_GET[id]";
    $i++;
}
$totalTime = microtime(true) - $startTime;
echo "Total Time: " . $totalTime, PHP_EOL;
<?php
$_GET['id'] = 'test';
$i = 0;
$startTime = microtime(true);
while ($i < 1000000) {
    $newURL = 'http://example.com/foo.php?id=' . $_GET['id'];
    $i++;
}
$totalTime = microtime(true) - $startTime;
echo 'Total Time: ' . $totalTime, PHP_EOL;
但是,如果您选择使用字符串浓缩运算符(
),最好使用单引号(
)。性能比使用双引号(
)要好

concat3.php

<?php
$_GET["id"] = "test";
$i = 0;
$startTime = microtime(true);
while ($i < 1000000) {
    $newURL = "http://example.com/foo.php?id=" . $_GET["id"];
    $i++;
}
$totalTime = microtime(true) - $startTime;
echo "Total Time: " . $totalTime, PHP_EOL;
<?php
$_GET["id"] = "test";
$i = 0;
$startTime = microtime(true);
while ($i < 1000000) {
    $newURL = "http://example.com/foo.php?id=$_GET[id]";
    $i++;
}
$totalTime = microtime(true) - $startTime;
echo "Total Time: " . $totalTime, PHP_EOL;
<?php
$_GET['id'] = 'test';
$i = 0;
$startTime = microtime(true);
while ($i < 1000000) {
    $newURL = 'http://example.com/foo.php?id=' . $_GET['id'];
    $i++;
}
$totalTime = microtime(true) - $startTime;
echo 'Total Time: ' . $totalTime, PHP_EOL;

可能会有非常微小的性能差异,但除此之外,这主要是基于意见的(因此脱离主题)。但是,在我看来,第一个选项当然更容易阅读。第一个选项肯定是最好的。它从未让我失望过。最好阅读,就像@Chris所说的:PHP变量的分隔性更好,当您使用
sprintf()时
很多时候,你也会看到为什么定界对可读性很有价值。至于性能:我真的不认为在这种情况下值得一提。哦,我不认为第二个选项不太常见。我看到很多用户在使用它。实际上仍然在使用它:-)我使用过的所有其他编程语言(我记得)将连接运算符作为唯一选项。