Jump to content

Recommended Posts

I don't know what I just cannot get about it.  There are a ton of posts on this and I just cannot realize when best to use double quotes and when to use single.  My questions are..

 

1.  Which requires less processing if you are NOT using a variable in say an echo like this..

<?
//Single quotes?? 
echo 'Hello World';
//or double quotes??
echo "Hello World";
?>

 

2.  What is the best way to use a variable (for performance reasons)...

<?
//Single quotes?? 
echo 'Hello $var';
//or double quotes??
echo "Hello ".$var;
?>

 

3.  What do you think would be the most efficient use of this code...

<?
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
{
        //how can I make this next line LESS ugly and fastest processing...
        echo "  <option>".$row['City'].", ".$row['State']."</option>\n";
}
?>

((note the above code works fine.. just looking for processing power/speed))

 

Any other general rules on when to use single vs double would be greatly appreciated.

 

Another random thing I thought of while writing this.. when I use double quotes and the "\n" it writes a blank line.  Is there a way to do that with single quotes?

To answer your questions:

 

"1.  Which requires less processing if you are NOT using a variable in say an echo like this.."

The php pre-compiler has become so advanced that you wouldn't notice a speed difference between the single and double quotes. However in the past it was well known to try steer clear of double quotes (or smart quotes). Single quotes were seen as not-so-clever quotes.

 

"2.  What is the best way to use a variable (for performance reasons)..."

For simplicity sake I would personally go for echo "hello $var"; instead of echo "hello".$var;. It just reads better imo. Again performance wise there is not much difference.

 

"3.  What do you think would be the most efficient use of this code..."

echo "  <option>$row['City'], $row['State']</option>\n";

 

And No, you cannot write a line-break with single quotes as it is seen as 'dumb' quotes. Opinions on these topics may differ but in principle use quotes in context.

echo '<option>',$row['City'],', ',$row['State'],'</option>', "\n";

 

Notice the , instead of . which is faster then concatenating the string

 

I second o3d altough I still use single-quotes for my normal strings and double-quotes whenever variables are involved to make it better readable (thus not really a performance issue but more a readability issue)

echo '<option>',$row['City'],', ',$row['State'],'</option>', "\n";

 

Notice the , instead of . which is faster then concatenating the string

 

Do you want to bet? Here is a random test I wrote awhile back:

http://zwap.to/0021K

 

It's the second test, it's quite slow comparitively on this kind of concatenation. Echo is a structure, It's pointless to use a structure on an unstructured string.

http://www.electrictoolbox.com/php-echo-commas-vs-concatenation/

 

echo $string1, $string2, $string3;

Approx average 2.08 seconds

 

echo $string1 . $string2 . $string3;

Approx average 3.48 seconds

 

I have done some searching on the web and it greatly differs some say it's faster others say it's slower

 

Sources:

http://hungred.com/useful-information/php-micro-optimization-tips/

http://www.phpbench.com/

http://www.simplemachines.org/community/index.php?topic=47171.0

From what I read in the PHP manual, I think they say that single quotes are not parsed by PHP and double quotes are. I gathered that means PHP will just handle strings in single quotes as they are - "no parsing". Double quotes it will swap out variable values etc.

 

For some reason, over the years of hacking, I've ended up adopting a policy to use single quotes for strings most of the time. This was usually because of needing to use double quotes inside strings a lot of the times. Regarding readability, there's a bit of a catch 22: do you use single quotes and then have to concat variables into your string all the time: $string = 'This is a '.$value; or do you use double quotes and lose the syntax highlighting of the variable in your IDE/editor?: $string = "This is a $value"; ($value will be the same colour as the rest of the string).

 

I ended up opting to use single quotes because of double quote horrors, and to preserve my syntax highlighting as well as giving me almost 100% consistancy through my code.

 

See: http://www.php.net/manual/en/language.types.string.php - there is a section called 'Variable Parsing', and also talks about double and single quote string.

The performance of "Hello ".$guy vs. "Hello $guy" will never made in any real-world situation. There isn't a right or wrong, so generally I just stick to what looks most readable.

 

The one advantage of writing it as "Hello ".$guy is that, should you need to call a function on $guy you don't have change as much (eg: "Hello ".ucwords($guy))

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.