-1

Is there a difference between

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

and

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

performance-wise? Is one better coding style then the other? It seems that the second option is less common. Is it because nobody knows that it actually works or because there is a downside of this method?

Dolly Aswin
  • 2,684
  • 1
  • 20
  • 23
Felix Bernhard
  • 396
  • 6
  • 24
  • 1
    There may be very tiny performance differences, but otherwise this is primarily opinion-based (and therefore off-topic). However, in my opinion the first option is certainly easier to read. – ChrisGPT was on strike Jul 03 '17 at 00:44
  • Definitely the first one is the best one to use. It never disappointed me. It's better to read, like @Chris said: The PHP vars are much better delimited and, when you'll use `sprintf()` a lot, you'll also see why delimitation is valuable to the readability. As for the performance: I really don't think it's worth to be mentioned in this case. Oh, and I don't think that the second option is less common. I saw a lot users on SO using it. Actually still using it :-) All other programming languages with which I worked (as I recall) have the concatenation operators as the only option. –  Jul 03 '17 at 01:30

2 Answers2

1

Well, the first method is a string concatenation while the second one is string interpolation.

Couldn't find any documentation about it, but after some testing, it seems that interpolation is slightly faster. And when I say slightly I mean very little difference.

I ran it 150M times and the results are:

  • Concatenation: 13.905070066452 seconds

  • Interpolation: 12.485554933548 seconds

Here's the code I used for testing:

$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;

IMPORTANT:

However you should also take a look at this in which @Jack see the instructions generated and concluded that concatenation were in fact faster than interpolation.

henrique
  • 1,072
  • 10
  • 17
1

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
Dolly Aswin
  • 2,684
  • 1
  • 20
  • 23