Jump to content

Recommended Posts

I can't remember a time where I got \n to work since I started PHP a month ago.

 

I'm reading the book PHP and MySQL Web Development, and I'm following a tutorial where the author uses the new line break, I'm doing it just like him, for him it works for me it doesn't.

 

 

This one for example doesn't work for me:

 

<?php
echo "test test test \n Test test test";
?>

 

And on this one, which is from the book tutorial, the \t WORKS, but the \n doesn't:

 

$outputstring = $date."\t".$tiregty." tires \t".$oilgty." oil\t"
			.$sparkgty." spark plugs\t\$".$totalamount
			."\t". $address."\n";

 

Did something happen with a PHP update, or is it because of my php.ini file settings?

 

I'm using XAMPP with default settings. And I have PHP version 5.3.1 and the book I'm reading is written for PHP 5.3.

 

 

Link to comment
https://forums.phpfreaks.com/topic/225770-n-is-never-working-for-me/
Share on other sites

Ok thanks for letting me know guys, I didn't know about that, I checked the view source page, and there it is working.

 

In the book example, there should be data inserted into a txt file (for every order one line), but when I open the txt file manually every order is on the first line and not on its own lines individually, is that normal too?

 

Perhaps when I read the file and echo it out, it will be separated on its individual lines?

 

The code for the insertion of data into the txt file:

 

$outputstring = $date."\t".$tiregty." tires \t".$oilgty." oil\t"
			.$sparkgty." spark plugs\t\$".$totalamount
			."\t". $address."\n";

// Open file for appending
@ $fp = fopen("$DOCUMENT_ROOT/PAMWD/c01/orders.txt", 'ab');

flock($fp, LOCK_EX);

if (!$fp) {
echo "<p><strong>Your order could not be processed at this time.
		Please try again later.</strong></p></body></html>";
exit;
}

fwrite($fp, $outputstring, strlen($outputstring));
flock($fp, LOCK_UN);
fclose($fp);

echo "<p>Order written.</p>";

 

 

Perhaps when I read the file and echo it out, it will be separated on its individual lines?

 

Ok I think I just misunderstood the \n feature, I thought of it like <br /> .... I just tried to read the txt file and echo it out and it would display the entries nice and neatly on new lines.

 

Thanks for clearing this up for me!

 

 

 

Use an editor like notepad++ to edit PHP files, it will show new lines as they should be displayed regardless of wether it is a \n | \r or \r\n.

 

There are different variations of the new line code because for some reason Mac/Windows and *nix think their newline method is the greatest (guess, but seems pointless to me).

 

So Windows, using the native notepad wil only convert "\r\n" into a visible new line, since that is what it looks for.

Similar to mac, its own variation of notepad may only convert \r into newlines,

and *nix will convert \n.

 

Notepad++ doesn't care wether its an \n or \r or both it will treat it the same, and you can even convert between the various formats with 2 clicks of the mouse.

 

..

Also remember that the code that you echo to the browser is _not_ what you actually see, the browser "interprets" the "response" from the server which consists of headers and a body, the headers tell the browser things like what operating system, what encoding the page uses or even wether to cache the page to history or not or for how long etc, the body will contain the HTML, it is _always_ HTML (or sometimes XML for AJAX requests, but you dont _need_ to use XML) for webpages, wether designed for CSS or flash.

 

Hope this helps clear things up.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.