Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics Daily Exercise Program Combining Strings

Which kind of quotation marks should be used with concantenation?

In the . and .= examples within the video, the strings are contained within single quotation marks. Is there a reason for that? I was thinking from the previous video that double quotes would be the better go-to/default choice for containing strings, because of the ease of internal concantenation .... but the use of single-quotes in this video makes me wonder. Thanks!

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hi Stephanie,

I was using double in the past but am now using single in php. The reason is that if you use double quotes and then need to use double inside the string, you have to escape them. It is easier to cut and paste html into single quotes as you don't have to go through and escape all double quotes. The example below should make it clearer:

As far as I am aware, there is no difference between using single or double quotes for strings and or concatenation.

$htmlString .= '<a href="mywebsite.com" target="_blank">Link to my website.</a>';

// vs

$htmlString .= "<a href=\"mywebsite.com\" target=\"_blank\">Link to my website.</a>";

// more complex:

$htmlString = '<div id="container">'
               .'<ul>';
// loop
$htmlString .=   '<li class="link"><a href="#" class="">Link 1</a></li>';
// finish loop

$htmlString .=   '</ul>'
              .'</div>';

Thanks! Consider myself convinced. ;-)

Ryan Larsen
Ryan Larsen
3,172 Points

Hey, I was actually wondering the same thing after the video so thank you for that explanation! It clears things up well.

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

Single quotes are string literals where as most escape characters and $variable names will be displayed as is.

Double quotes parses through escape chars and variables.

$name = "Stephanie";

// single quotes
echo 'Hi $name. \n Nice to meet you!';
// 'Hi $name. \n Nice to meet you!

// double
echo "Hi $name. \n Nice to meet you!";
// Hi Stephanie
// Nice to meet you!