Solution to the invalid n newline character in the PHP string and the inability to wrap the newline_PHP Tutorial

WBOY
Release: 2016-07-13 10:34:54
Original
927 people have browsed it

For example, the following code:

Copy the code The code is as follows:
echo 'hellon';
echo 'world';
?>

The newline character n in the program will be output directly and the line break cannot be correct. The solution is to change the single quotes to double quotes:
Copy code The code is as follows:
echo "hellon";
echo "world";
?>

That’s it! In fact, it is the difference between double quotes and single quotes in PHP. In a simple summary, variables in double quotes can be parsed, and single quotes are absolute strings.

Attach: Codes for three methods of removing newlines in PHP

Copy code The code is as follows:
//php Line breaks for different systems
//Different systems The implementation of line breaks is different.
// /n is used in Linux and Unix.
// MAC uses /r.
// In order to reflect the difference between window and Linux, /r/n is used.
//So the implementation methods are different on different platforms
//php has three ways to solve it

//1. Use str_replace to replace newlines
$str = str_replace(array("/ r/n", "/r", "/n"), "", $str);

//2. Use regular replacement
$str = preg_replace('//s*/ ', '', $str);

//3. Use variables defined by PHP (recommended)
$str = str_replace(PHP_EOL, '', $str);
? >

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/748678.htmlTechArticleFor example, the following code: Copy the code as follows: ?php echo 'hellon'; echo 'world'; ? Program The newline character n in will be output directly and cannot be correctly wrapped. The solution is to change the single quotes...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template