Why “\” gives error while storing it in string in PHP?

前端 未结 4 1233
灰色年华
灰色年华 2021-01-22 12:40

My string looks like:

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

If I replace / with \\, it gives error:

Expecting identifier

4条回答
  •  醉酒成梦
    2021-01-22 12:49

    \ 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.

提交回复
热议问题