Jump to content

Recommended Posts

The first one uses concatenation: you start with one string, concatenate the variable, then concatenate a second string. The dot there is the operator that does string concatenation.

The second one uses variable interpolation: PHP looks at what's in the string, sees a variable, and automatically "inserts" its value into that spot.

Using double quoted strings uses more processing than single quoted. If you're not putting a variable in them, there's no reason to use double quoted strings. If you are putting a variable in the string, sometimes you want to use " quotes in it.

Now which looks better?

<?php
$str = "<div id=\"myDiv\"><p class=\"myClass\">This is a string with one $var in it. Someone said, \"This is a silly string\"</p></div>";
$str = '<div id="myDiv"><p class="myClass">This is a string with one '.$var.' in it. Someone said, "This is a silly string"</p></div>';
?>

And if you're specifying a varibale from an array you cannot use it within double quotes.

 

Wrong.  You just need to use curly braces around the array variable:

 

echo "Look, I'm an array variable in a double-quoted string!  {$arr['someKey']}";

Using double quoted strings uses more processing than single quoted. If you're not putting a variable in them, there's no reason to use double quoted strings.

1. A negligible amount. It's a matter of a handful of added character comparisons (like character == '\\' || character == '$') which sum up to a grand total of negligible.

2. If you're concerned about efficiency, don't use PHP. There are plenty of other, real issues to focus on instead of which quoting style for strings is better.

3. I used to insist on single-quotes for variableless strings and double-quotes for other strings. Then I got tired of having to think ahead of time about whether I was going to put a variable into a string or not.

That's of course true regarding the efficiency ;)

 

Mostly I was pointing out it's a personal preference. You can't say definitively one is better than the other. It's like comparing

if(true){

}

to

if(true)

{

}

 

People will have preferences on them and what you think looks better may look ugly to someone else. Just do what works for you and be consistent.

I use single quotes for everything except SQL queries or other things I know have to have a ' in them. I have no problem going and changing a ' to a " so I don't worry about it too much ahead of time.

It really does come down to personal preference. I usually concatenate out of habit. It's taking enough effort sometimes to remember, should I be using a dot (.) or plus (+) to concatenate (PHP or JavaScript) let alone remember if a can use inline variables. It really comes down to, there is more than one way to do the same thing.

 

All the following do the same thing, and I've used them all at one time or another depending on mood:

echo 'This is my "text"!';
echo "This is my \"text\"!";
?>This is my "text"!<?php

I use single quotes for everything except SQL queries or other things I know have to have a ' in them. I have no problem going and changing a ' to a " so I don't worry about it too much ahead of time.

 

Why not use double quotes within query strings? SELECT `col` FROM `table` WHERE `val` = "string" is perfectly acceptable. Any escaping functions should treat single and double quotes the same.

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.