1

What is difference between \ and \\?

 echo $path_1="C:\wamp\www\practice\";

Output shows error:

Parse error: syntax error, unexpected $end, expecting T_VARIABLE or T_DOLLAR_OPEN_CURLY_BRACES or T_CURLY_OPEN

 echo $path_1="C:\\wamp\\www\\practice\\";

Output:

C:\wamp\www\practice\

Instead of printing C:\\wamp\\www\\practice\\ it prints C:\wamp\www\practice\

0

2 Answers 2

5

Typically in programming the character \ used between normal double quotes " is an escape character for special characters, making it a special character. So to display the \ you have to escape it with another \.

I would also mention that the reason you received a Parse Error is because the ending \ you used in your first example escapes the double quote so the end of line could not be parsed.

2

Try putting string in single quotes (')

echo $path_1='C:\wamp\www\practice\ '; //notice the space in the end

String inside " is checked for escape sequences and php variables. But string in ' is echoed as it is. So escape all \ when using inside " or write your string in '.

Important note string in ' cannot end with a \. This would escape out the closing quote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.