Solution to invalid line breaks and line breaks in php strings _ PHP Tutorial

Source: Internet
Author: User
The n linefeed in the php string is invalid and cannot be wrapped. For example, the following code :? Phpechohellon; echoworld ;? The linefeed n in the program will be directly output and cannot wrap the line correctly. the solution is to change the single quotes to the following code:

The code is as follows:

Echo 'hello \ n ';
Echo 'world ';
?>


The line break \ n in the program will be directly output and cannot wrap correctly. the solution is to change single quotes to double quotation marks:

The code is as follows:

Echo "hello \ n ";
Echo "world ";
?>


That's all! It is actually the difference between PHP double quotes and single quotes. Simply put, variables in double quotes can be parsed. single quotes are absolute strings.

Appendix: three methods for removing line breaks in PHP

The code is as follows:

// Line feed for different php systems
// The implementation of line feed varies with systems.
// In linux and unix/n
// For MAC/r
// Window is/r/n to reflect differences from linux
// The Implementation methods on different platforms are different.
// Php has three solutions

// 1. use str_replace to replace the line feed.
$ Str = str_replace (array ("/r/n", "/r", "/n"), "", $ str );

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

// 3. use the variables defined in php (recommended)
$ Str = str_replace (PHP_EOL, '', $ str );
?>

The pipeline code is as follows :? Php echo 'hello \ n'; echo 'world ';? The line break \ n in the program will be directly output and cannot wrap correctly. the solution is to change the single quotes...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.