chaseman Posted January 26, 2011 Share Posted January 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225770-n-is-never-working-for-me/ Share on other sites More sharing options...
ChemicalBliss Posted January 26, 2011 Share Posted January 26, 2011 Have you checked the source-code, can you give us an example of the "view Source" of your results? Quote Link to comment https://forums.phpfreaks.com/topic/225770-n-is-never-working-for-me/#findComment-1165587 Share on other sites More sharing options...
Pikachu2000 Posted January 26, 2011 Share Posted January 26, 2011 What is it that you're expecting the \n to do? Quote Link to comment https://forums.phpfreaks.com/topic/225770-n-is-never-working-for-me/#findComment-1165590 Share on other sites More sharing options...
Cagecrawler Posted January 26, 2011 Share Posted January 26, 2011 If you're viewing it in a browser, \n doesn't show in the formatted output. You need to use <br/> instead. \n should work if you run the script on the command line or if you view the source of the web page. Quote Link to comment https://forums.phpfreaks.com/topic/225770-n-is-never-working-for-me/#findComment-1165594 Share on other sites More sharing options...
Maq Posted January 26, 2011 Share Posted January 26, 2011 \t shouldn't work either. Like Cagecrawler mentioned, they will both show up in view source or if you run the script in the CLI. Quote Link to comment https://forums.phpfreaks.com/topic/225770-n-is-never-working-for-me/#findComment-1165603 Share on other sites More sharing options...
chaseman Posted January 26, 2011 Author Share Posted January 26, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/225770-n-is-never-working-for-me/#findComment-1165624 Share on other sites More sharing options...
ChemicalBliss Posted January 27, 2011 Share Posted January 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225770-n-is-never-working-for-me/#findComment-1166105 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.