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

Ben Os
Ben Os
20,008 Points

What did our beloved Alena Holligan meant here in the PHP basics course?

https://teamtreehouse.com/library/escape-sequence

In the last 15 seconds here, Alena says:

Escape sequences are primarily only for double quoted strings,

  • I would humbly ask why primarily? They could use for doublequotes-inside-doublequotes as they could use singlequotes-inside-singlequotes, for example, when either internal quote includes $some_var.

Single quoted strings interpret each character individually.

  • I didn't understand that... AFAIK, characters themselves don't interpret the code, rather, a language engine does so, so I believe there is a slightly different meaning here?

Alena Holligan Nick Pettit

1 Answer

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

It's because in PHP the only character you need to escape in single-quoted strings, is the single quote. In double-quoted string you could have to escape: $ (dollar sign), double quote, special characters (\n, \t, \u2003)

In single quoted string, PHP does not interpret them. That's why she says (in my opinion) that single-quoted string are evaluated character by character.

Alena Holligan
Alena Holligan
Treehouse Teacher

yes, exactly Christopher Debove ! The only escape sequence that is used in a single quoted string is for a single quote (\'). Every other character in a single quoted string is used literally. Therefore "Escape sequences are primarily only for double quoted strings".

For example: If you had a single quoted string as follows

$myVariable = 'test';
echo 'The value of \$myVariable is \n $myVariable';

The results would be:

The value of \$myVariable is \n $myVariable

Whereas if you had the same string with double quotes

$myVariable = 'test';
echo "The value of \$myVariable is \n $myVariable";

The results would be

The value of $myVariable is 
test