phpescapingspecial-characters

Why does "\" give an error while storing it in string in PHP?


My string looks like:

$fullPath = $dirName . "/" . $file;

If I replace / with \, it gives error:

Expecting identifier or variable

I want to store in the latter way itself. How to override anything coming in between?


Solution

  • \ is an escape character. It is a special character used to define either escape sequences or to escape string-delimiters if you want to use them in the string itself.

    The correct usage is to escape the escape character like this: \\. This will tell the parser the you wanted the literal backslash and not the special meaning of it.