We all know the line break in PHP: \ n, carriage return: \ r, when you need to change lines, it's usually a combination of "\ r \ n". But why does the newline character not work when we write a file using fwrite? Let's look at the following example:
<?php
$filename = ' file.txt ';
$word = ' Hello!\r\n Welcome to www.111cn.net ';
$fh = fopen ($filename, "a"); W writes a append write from the beginning
Echo fwrite ($fh, $word);
Fclose ($FH);
?>
A carriage return line character "\ r \ n" is added to the $word string, but the output is not expected, and the carriage return line character "\ r \ n" is not resolved to be a newline character, but is directly treated as a char output.
Why is this happening? After study, the original is a single double quotation mark! We put the single quotation mark "'" of the $word definition string into double quotes "". The correct wording is as follows:
$filename = ' file.txt ';
$word = "Hello!\r\n Welcome to Www.111cn.net";
$fh = fopen ($filename, "a"); W writes a append write from the beginning
Echo fwrite ($fh, $word);
Fclose ($FH);
In the above example, Echo fwrite () shows a number that represents the length of the string.
Knowledge expansion
Using double quotes (") to define a string, PHP knows more about the escape sequence for special characters:
Transfer sequence Description
\ n Line Change
\ r Carriage Return
\ t Horizontal tab
\[/td> Backslash
\$ dollar Sign
\ "Double Quotes
\[0-7]{1,3} This regular expression sequence matches a character represented by a octal symbol
\x[0-9a-fa-f]{1,2} This regular expression sequence matches a character represented in hexadecimal notation