Single Quotes Or Double Quotes With PHP?
I have heard tales from the internet about how to write efficient PHP code, and the fact that writing and output string with " (double quote) is slower than writing one with a ' (single quote). The reason is that when PHP encounters a double quote is parses the string to see what is inside. PHP doesn’t do this with single quotes and so the code will run slightly faster.
Rather than take this on face value I thought I would run a benchmark on the code to see what is the quickest way of printing a variable is using the echo command. There are lots of ways to print out a single string using echo, they are as follows.
- Double quoted string = echo "before variable $variable after variable";
- Double quoted escaped string = echo "before variable ".$variable." after variable";
- Double quoted string with commas = echo "before variable ",$variable," after variable";
- Single quoted escaped string = echo 'before variable '.$variable.' after variable';
- Single quoted string with commas = echo 'before variable ',$variable,' after variable';
- Escaped HTML = ?> before variable <?php echo $variable; ?> after varaible <?php
Using the PHP benchmarking function I tested each of these mechanisms to see which was the fastest. Because each test is very quick I ran them all 10,000 times just to get a really good average time. Here is the code for the tests.
<?php
function getmicrotime($t){
list($usec, $sec) = explode(" ",$t);
return ((float)$usec + (float)$sec);
}
$results = array();
$variable = 'the variable value';
for($i=0;$i<=10000;++$i){
$start = microtime();
echo "before variable $variable after variable";
$end = microtime();
$results['double quotes'][] = (getmicrotime($end) - getmicrotime($start));
$start = microtime();
echo "before variable ".$variable." after variable";
$end = microtime();
$results['double escaped quotes'][] = (getmicrotime($end) - getmicrotime($start));
$start = microtime();
echo "before variable",$variable,"after variable";
$end = microtime();
$results['double quotes and commas'][] = (getmicrotime($end) - getmicrotime($start));
$start = microtime();
echo 'before variable '.$variable.' after variable';
$end = microtime();
$results['single escaped quotes'][] = (getmicrotime($end) - getmicrotime($start));
$start = microtime();
echo 'before variable',$variable,'after variable';
$end = microtime();
$results['single quotes and commas'][] = (getmicrotime($end) - getmicrotime($start));
$start = microtime();?>
before variable <?php echo $variable; ?> after variable
<?php $end = microtime();
$results['html'][] = (getmicrotime($end) - getmicrotime($start));
}
When all this is finished I had a bunch of arrays with lots of times in them. I used the following bit of code to work out the average and see which is the fastest.
foreach($results as $method=>$time){
$average = number_format((array_sum($time)/count($time)),10);
$fastestaverage[$method]=$average;
}
// sort the averages
krsort($fastestaverage);
echo '<pre>'.print_r($fastestaverage,true).'</pre>';
This produced the following result.
Array
(
[double quotes and commas] => 0.0000049084
[double escaped quotes] => 0.0000049590
[single quotes and commas] => 0.0000050394
[double quotes] => 0.0000053428
[single escaped quotes] => 0.0002022830
[html] => 0.0002147275
)
So it looks as though the quickest way to print out a string using the echo command is to use a combination of double quotes and commas. Like the following snippet.
echo "before variable",$variable,"after variable";
Either I am doing something wrong here, or PHP doesn’t work like I thought it does. Can anyone shed any light on why this result occurs? I know that there are only microseconds between one result and the next, but I would appreciate any information.
Comments
Pingback from Look At PHP Execution Times With PHP Benchmark | Talk In Code
Date: July 15, 2008, 8:23 am
[...] Single Quotes Or Double Quotes With PHP? [...]
Comment from myname
Date: September 17, 2008, 3:56 pm
Well,for one you do not measure the time php take to parse that code; then it’s only going to be parsed once in any case. So what you are measuring can not show the effect you are looking for (quote The reason is that when PHP encounters a double quote is parses the string to see what is inside endquote). Finally the concatenation operator is going to be called 20 000 times.
I must admit I do not understand why the latter doesnot affect your “double escaped quotes”
Write a comment