Whether you are writing JavaScript or PHP, you are always accustomed to using single quotes. But I encountered a problem while coding at home on the weekend. I need to filter the line breaks in the string through PHP. Follow the following method:
$out = str_replace(array('rn', 'r', 'n'), '', $out);PHP provides three ways to define strings: single quotes, double quotes, local documents (English called here document or heredoc).
Single quotation mark:
Using single quotes is the most efficient method, because PHP does not check built-in variables and escape sequences in single-quoted strings. The only characters that need to be escaped are backslashes and single quotes themselves.
Double quotes:
Built-in variables and escape sequences are checked, but escaped single quotes are not recognized. This also shows what is wrong with the code at the beginning. The correct approach is to use double quotes to define the escape sequence for newlines:
$out = str_replace(array("rn", "r", "n"), '', $out);Local document:
Check all built-in variables and escape sequences. Double quotes do not need to be escaped. For example:
echo <<
This is a "here document" example.
Just for test.
EOT; Simply record it to deepen your impression.