<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Single versus double quotes.

Published on
5,587 Points
2,587 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

Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Join & Write a Comment

Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month