This string is handled differently in PHP and JS, i.e. you get different results.
The only sequences of screens in single quotes in PHP are \\ and \' . All the rest are displayed literally, according to the documentation :
To specify a literal single quote, execute it with a backslash ( \ ). To specify a literal backslash, double it ( \\ ). All other instances of the backslash will be treated as a literal backslash: this means that other escape sequences that you might get used to, such as \r or \n , will be issued literally, as indicated, and not have any special value.
In JS, on the other hand, if the string contains an invalid escape sequence, the backslash is discarded ( CV stands for character value):
- CV CharacterEscapeSequence :: NonEscapeCharacter is a CV NonEscapeCharacter.
- CV NonEscapeCharacter :: SourceCharacter, but not EscapeCharacter or LineTerminator - this is the SourceCharacter character itself.
The quote may not be practical on its own, but if you follow the link and look at the grammar, it should become clear.
So, in PHP, the string will literally contain \" , while in JS it will contain only " , which makes it invalid JSON:
{"data":"{"json":"rocks"}"}
If you want to create a literal backslash in JS, you need to avoid it:
'{"data":"{\\"json\\":\\"rocks\\"}"}'
source share