<

Single versus double quotes.

Published on
5,555 Points
2,555 Views
Last Modified:
Chris Gralike
Not too political, always looking for the best advice or solution, and if need be using a pragmatic approach.
Everyone programming PHP or most of the other languages should at least understand the difference between the single quote and double quote.

in an coded example;
 
<?php
$a = 1;
echo '$a';   // String Literal
echo "$a";   // variable expansion and escape intepretation.
?>

Open in new window


Will output;
$a1

Where $a is the string literal ( echo '$a'; ) and 1 is the expanded $a that had the value 1.

Logically using the " " will make php interpret the vars used inside the quotes, but another thing about PHP is that its weakly typed. This means we don't "declare" data-types. Now how will PHP perform using these two in conjunction.

This means the Code we write is in fact valid PHP code, and as PHP is still weakly written we might measure the performance of the engine using the right code...

So my first test was quite simple, i wanted to echo a string telling me '$x is not a declared var.... To test the difference i wrote the following code.

 
<?php 
// Example using double quotes 
$sm = microtime(true);
$data = "\$a is not a declared var";
$hm = microtime(true);
echo $data;
$time1 = $hm - $sm;
echo $time1; 
		
// Example using single qoutes 
$sm = microtime(true);
$data =  '\$a is not a declared var';
$hm = microtime(true);
echo $data;
$time2 = $hm - $sm;
echo $time2; 
$diff = $time1 - $time2; 
echo '<br>'.$diff; 
?>

Open in new window


Because micro time contains such small numbers other processes on the machine might actually be "notable" inside the results, so these are the results of a few refreshes.
Amounts single ' where quicker then double "

1.0967254638672E-5
1.0251998901367E-5
8.1062316894531E-6
1.215934753418E-5

If you ask me the difference in performance between singles and doubles in such a simple string are remarkable. Naturally the amount of work the PHP Engine needs to do to figure out what exactly is asked is immense in comparison.

Now this was the simple example of the escape char? now what about a string build-up we see allot inside various PHP applications? The one I am talking about is the famous double quotes inside SQL statements for example.

You prob. all have seen its structure somewhere.
 
<?php
$sql = "UPDATE table
        SET username = $username, password = $password
        WHERE userid = $userid";
if(mysql_query($sql)){
   \\....
?>

Open in new window


Well i wrote this in a way and measured the time php needed to execute the code. Here is the code I used.

 
<?php 
$a = 1;
$b = 2;
$c = 3;
$d = 6; 
// Example using double quotes
$sm = microtime(true);
$data = "$a + $b + $c = $d <br />";
$hm = microtime(true);
echo $data;
$time1 = $hm - $sm;
echo $time1; 
echo '<br/>';
		
// Example using single qoutes
$sm = microtime(true);
$data =  $a . ' + ' . $b . ' + ' . $c . ' = ' . $d . '<br />';
$hm = microtime(true);
echo $data;
$time2 = $hm - $sm;
echo $time2; 
$diff = $time1 - $time2; 
echo '<br>'.$diff; 
?>

Open in new window


Well you might wonder about the results? Here are the measurements during a few refreshes.
1.2874603271484E-5
1.5974044799805E-5
1.4305114746094E-5
2.3841857910156E-5

Conclusion.

Even though the differences are small, in huge applications it might be worth writing your code as clean as possible and it might improve the performance over time. Realize what the goal of each declaration is and use the best declaration method for that goal.

Be honest, readability might be an issue but in the end it about how well your application performs and how stable your application operates, not how "readable" your code is composed. ;-)

Rgrds,
0
Comment
0 Comments

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Join & Write a Comment

The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month