View Full Version : i dont understand with both slash (/) and backslash (\) usage.

08-12-2010, 02:55 PM
i dont understand with both slash (/) and backslash (\) usage.
what is the different between / and \ ?

i know the both slash how to use, but not sure backslash.
any explanation about it?

if ($error != "") {
echo "<div class=\"err\">$error</div>";

08-12-2010, 03:31 PM
if you look at your example

echo "<div class=\"err\">$error</div>";you are defining a string. A bit of text.

You define a string in PHP by wrapping it in quotes ""

In your example your string is HTML and it contains attributes and attributes have values wrapped in quotes. You need a way to tell PHP not to treat the quotes in the HTML as quotes. They are just part of the string so when you add \ you are switching the power of the quote off. This tells PHP to ignore the quote after the \. We call this escaping the quote.

If you didn't escape the quote the code would fail...

//PHP interprets this as 1 string + err (syntax error) + >$error</div> (syntax error)
echo "<div class=" err ">$error</div>";

PHP would register the first quote and would expect a string to follow. It would think you have finished with the string when it found the next quote class=" which is the next quote in line. It would think that was a terminating quote ending the string. Now you have err which would be interpreted as being outside the quote and therefore it would be treated as code. PHP would do a 'WTF' and throw an error as it wont know what to do with err

08-13-2010, 06:20 AM