+ 1

what is the output of this code and why?

$str = 'a\\b\n'; echo $str; Does PHP print new lines in echo or always print \n

8th Oct 2018, 7:32 PM
Khaled Arnaout
Khaled Arnaout - avatar
1 Answer
+ 3
The output of the code is a\b\n. Why? Because you are using single quotes. With single quotes in PHP, to specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning. If you want \n to be treated like a newline use double quotes (").
8th Oct 2018, 9:09 PM
Ulisses Cruz
Ulisses Cruz - avatar