I have the following test script:
<?php $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = "Floppy Jalopy\n"; fwrite($fh, $stringData); $stringData = "Pointy Pinto\n"; fwrite($fh, $stringData); fclose($fh); ?>
when run however and opened usign Notepad, the data is returned in a single line without breaks as:
Floppy Jalopy(crazy box)Pointy Pinto(crazy box)
where i cant find the appropriate character for 'crazy box' but its a REALLY crazy box. WHAT GIVES!
Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.
CR and LF are control characters or bytecode that can be used to mark a line break in a text file. CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line.
The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.
It is an in-built function of PHP, which is used to insert the HTML line breaks before all newlines in the string. Although, we can also use PHP newline character \n or \r\n inside the source code to create the newline, but these line breaks will not be visible on the browser.
It is best to use PHP_EOL
. This is cross-platform, so it automatically chooses the correct newline character(s) for the platform PHP is currently running on.
$stringData = "Floppy Jalopy" . PHP_EOL;
PHP Constants
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With