This article shares a problem I encountered with double quotes and single quotes in PHP during my actual work, along with a solution. Friends in need can use it as a reference.
To filter newlines in strings through PHP, you usually do this:
<?php $out = str_replace(array('\r\n', '\r', '\n'), '', $out); Copy after login
PHP provides three ways to define strings: single quotes, double quotes, and local documents (called here document or heredoc in English).
apostrophe:
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:
<?php $out = str_replace(array("\r\n", "\r", "\n"), '', $out);
heredoc: Copy after login
Check all built-in variables and escape sequences, double quotes do not need to be escaped.
For example:
<?php echo <<<EOT
this is a "here document" example.
just for test.
EOT; Copy after login
|