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

前端 未结 4 1190
灰色年华
灰色年华 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.

    0 讨论(0)
  • 2021-01-22 12:58

    You need to escape the slash: $fullPath = $dirName . "\\" . $file;

    0 讨论(0)
  • 2021-01-22 12:59

    You might use apostrophes to denote a non-parsed string:

    $fullPath = $dirName . '\' . $file;
    
    0 讨论(0)
  • 2021-01-22 13:09

    You need "\\" because \ is an escape character.

    0 讨论(0)
提交回复
热议问题