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 String Manipulation

Miguel Nunez
Miguel Nunez
3,266 Points

what is the purpose of escape sequences in general

I know there are different ones out there but its really hard to find sources on google that makes sense to newbies like me to understand because they use a lot of assume languages and big fancy words.

index.php
<?php

//Place your code below this comment

?>

2 Answers

hassan alzahrani
hassan alzahrani
3,623 Points

Some times you have requirements to use them and for that you have this option. As an example: you want to write the following string:

Hello, I am Hassan /\ /\, to access the file please go to the following directory C:\program File\The file.

In the this example I have to write four escape characters in PHP, otherwise It wont work.

Please check this link to read more about other use of them.

Miguel Nunez
Miguel Nunez
3,266 Points

So are you saying that they are used for different character structures ? Like grammar quotes etc...?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

They're used to make characters that wouldn't otherwise be able to be added because they'd break the string.

echo "My name is William "The Conqueror" ";

If I tried to use that string it would be broken by the second quotation mark because PHP thinks that is the end of the string and doesn't know why the rest of the chracters in the string.

If you want to include the rest of the characters you need to precede it with a backslash.

echo "My name is William \"The Conqueror\" ";

Now the quotes should be displayed as part of the string.

Miguel Nunez
Miguel Nunez
3,266 Points

So are you saying that they are used for different character structures ? Like grammar quotes etc...?

hassan alzahrani
hassan alzahrani
3,623 Points

They are used to tell PHP to not to consider the next character as explained by Mr.Jonathan.

Before the double quotations of the "The Conqueror" he added backslash to make PHP understand that is part of my string and PHP need to ignore it.