Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does nobody use single quotes? 1

Status
Not open for further replies.

scorpion4377

Programmer
Apr 11, 2005
13
US
Everybody uses double quotes, from the people at PHP.net to some people who make big open-source projects. But why? Its MUCH slower, and using single quotes in more professional. Why doesn't everybody use single quotes?

Just wondering. Sorry if this is off-topic.
 
I use more single quotes than double quotes. I will use double quotes to contain strings that contain single quotes, but rarly anytime else.

Ken
 
I use all single quotes, and I never plan on using double quotes.
 
I use single quotes except for strings that contain single quotes or escaped characters: \n, \t, \r, etc.

Lee
 
scorpion4377 said:
Its MUCH slower..

why? I didn't know that... I use simple or double quote depending the string to print (if it contains ' or " chars). But I didn't know that using double quotes is slower that simple quote. It¡s god to know it (thanks), but what is the reason.

Thanks.
 
It would be nice to back up the quantification "MUCH" with some real data. Intuitively we all understand that a string that is parsed for possible variables will be taking more time than a constant which is taken as is.
However, HOW MUCH slower might be an insignificant amount of time.
Using the following test code (partly extracted from the PHP manual):
Code:
<?php
/*
compares runtimes of 2 functions
to use, copy/paste functions, and call them from fct1 and fct2

*/

$runtimes=100;//how many times to run the tests
function fct1(){
   for($j=0; $j<10000; $j++) {
   	   $str .= '!';
   }
}
function fct2(){
   for($j=0; $j<10000; $j++) {
   	   $str .= "!";
   }
}

//################################################################//
$time1=0;
for ($i=0;$i<$runtimes;$i++){
   $time_start = array_sum(explode(' ', microtime()));
   fct1();
   $time_end = array_sum(explode(' ', microtime()));
   $time1 = ($time1*$i+$time_end - $time_start)/($i+1);
}
echo "\nfct1 runs in $time1 ms.\n";
$time2=0;
for ($i=0;$i<$runtimes;$i++){
   $time_start = array_sum(explode(' ', microtime()));
   fct2();
   $time_end = array_sum(explode(' ', microtime()));
   $time2 = ($time2*$i+$time_end - $time_start)/($i+1);
}
echo "\nfct2 runs in $time2 ms.\n";
$tf = round($time2/$time1);
echo "\nfct".(($tf>1)?'1 is faster by a factor of '.$tf:(($tf<1)?'2 is faster by a factor of '.round($time1/$time2):'1 is roughly equal to fct2'))."\n\n";
?>
Result for 100 runs of each function concatenating a string 10K times:
fct1 runs in 0.016148986816406 ms.
fct2 runs in 0.016058847904205 ms.
fct1 is roughly equal to fct2

Result for 100 runs of each function concatenating a string 10K times fct2 with variable substitution:
fct1 runs in 0.020972301959991 ms.
fct2 runs in 0.025942962169647 ms.
fct1 is roughly equal to fct2

Result 100 runs of each function (same as above) but three substitutions in fct2 (three vars in the double quotes):
fct1 runs in 0.017860918045044 ms.
fct2 runs in 0.039006175994873 ms.
fct1 is faster by a factor of 2

So the statement should be:
Double quotes are only significantly slower when actual variable substitution takes place. The first test actually indicates thaqt double quotes without variable substitution are actually faster. However, if you print non dynamic strings with single or double quotes makes virtually no difference.
 
hmm... It's a very good explanation DRJ478, so -to be realistic- it doesn't matter whether you use single o double quotes... I guess I going to continue using them as ussual.

Cheers and thanks a lot for explanation DRJ.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top