之间有区别吗?
$newURL = "http://example.com/foo.php?id=" . $_GET["id"];
和
$newURL = "http://example.com/foo.php?id=$_GET[id]";
性能代价?是另一种更好的编码风格吗?似乎第二种选择不太常见。是因为没有人知道它确实有用,或者因为这种方法存在缺点?
答案 0 :(得分:1)
嗯,第一种方法是字符串连接,而第二种方法是字符串插值。
无法找到有关它的任何文档,但经过一些测试后,似乎插值稍快一些。当我稍微说一点时,我的意思是差别很小。
我跑了 150M 次,结果是:
连接: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;
但是你也应该看一下this,其中@Jack看到生成的指令,并得出结论,连接实际上比插值更快。
答案 1 :(得分:1)
他们之间的表现没有太大差异。我使用AWS EC2 T2 Micro实例测试了它。脚本
<强> concact1.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;
<强> concat2.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;
结果如下:
dolly@ip-172-31-xx-xxx:~$ php concat1.php
Total Time: 0.13097405433655
dolly@ip-172-31-xx-xxx:~$ php concat1.php
Total Time: 0.13607907295227
dolly@ip-172-31-xx-xxx:~$ php concat1.php
Total Time: 0.13074207305908
dolly@ip-172-31-xx-xxx:~$ php concat1.php
Total Time: 0.13133096694946
dolly@ip-172-31-xx-xxx:~$ php concat1.php
Total Time: 0.13713884353638
dolly@ip-172-31-xx-xxx:~$ php concat1.php
Total Time: 0.13053917884827
dolly@ip-172-31-xx-xxx:~$ php concat1.php
Total Time: 0.13039898872375
dolly@ip-172-31-xx-xxx:~$
dolly@ip-172-31-xx-xxx:~$ php concat2.php
Total Time: 0.16021704673767
dolly@ip-172-31-xx-xxx:~$ php concat2.php
Total Time: 0.13897895812988
dolly@ip-172-31-xx-xxx:~$ php concat2.php
Total Time: 0.13820290565491
dolly@ip-172-31-xx-xxx:~$ php concat2.php
Total Time: 0.14002299308777
dolly@ip-172-31-xx-xxx:~$ php concat2.php
Total Time: 0.13921785354614
dolly@ip-172-31-xx-xxx:~$ php concat2.php
Total Time: 0.13863801956177
dolly@ip-172-31-xx-xxx:~$ php concat2.php
Total Time: 0.13729095458984
但是,如果您选择使用字符串连接运算符(.
),最好使用单引号('
)。性能优于使用双引号("
)。这里的脚本
<强> 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;
结果如下:
dolly@ip-172-31-xx-xxx:~$ php concat3.php
Total Time: 0.12913393974304
dolly@ip-172-31-xx-xxx:~$ php concat3.php
Total Time: 0.13022017478943
dolly@ip-172-31-xx-xxx:~$ php concat3.php
Total Time: 0.12958312034607
dolly@ip-172-31-xx-xxx:~$ php concat3.php
Total Time: 0.12940907478333
dolly@ip-172-31-xx-xxx:~$ php concat3.php
Total Time: 0.12957501411438
dolly@ip-172-31-xx-xxx:~$ php concat3.php
Total Time: 0.13019990921021