There is no much difference in performance between them. I have test it using AWS EC2 T2 Micro instance. Here the scripts
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;
And here the results:
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
But, if you choose to use string concatentation operator (.
), better to use single quote ('
). The performance is better than use double quotes ("
). Here the script
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;
Here is the result:
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