Jump to content

New Line not working!


Recommended Posts

Hi,I have Apache2Triad installed on my PC and I'm trying to learn PHP with the PHP manual.I've copied several examples into .php files and run them in Apache.The new line command "\n" doesn't produce a new line. I've looked through the php.ini file to see if I need to set a directive, but have been unable to find it.Could someone help direct me to a resolution of this issue?Bill

Link to post
Share on other sites
The new line command "\n" doesn't produce a new line.
If by this you mean a line break in the middle of an HTML page, that is normal behavior. To get a line break you'll need to explicity echo a <br> or <p> tag.If it's some other context, and "\n" shows up as "\n" you may need to escape the escape character, thus: "\\n". I'm really guessing because you didn't explain a whole lot.
Link to post
Share on other sites

Here is an example of the problem, forgive me for not including it in my original post:

<?php	   define('EPSILON', 1.0e-8);	   function real_cmp($r1, $r2)	   {			   $diff = $r1 - $r2;			   if( abs($diff) < EPSILON )					   return 0;			   else					   return $diff < 0 ? -1 : 1;	   }	   function real_lt($r1, $r2)	   {			   return real_cmp($r1, $r2) < 0;	   }	   echo "raw compare\n";	   $n = 0;	   for($i = 0.1; $i < 1.0; $i += 0.1) {			   $n++;			   echo "$i\t$n\n";	   }	   echo "\nepsilon compare\n";	   $n = 0;	   for($i = 0.1; real_lt($i, 1.0); $i += 0.1) {			   $n++;			   echo "$i\t$n\n";	   }/*	   Outputs:	   raw compare	   0.1	 1	   0.2	 2	   0.3	 3	   0.4	 4	   0.5	 5	   0.6	 6	   0.7	 7	   0.8	 8	   0.9	 9	   1	   10	   epsilon compare	   0.1	 1	   0.2	 2	   0.3	 3	   0.4	 4	   0.5	 5	   0.6	 6	   0.7	 7	   0.8	 8	   0.9	 9*/?>

This is the output I am getting:raw compare 0.1 1 0.2 2 0.3 3 0.4 4 0.5 5 0.6 6 0.7 7 0.8 8 0.9 9 1 10 epsilon compare 0.1 1 0.2 2 0.3 3 0.4 4 0.5 5 0.6 6 0.7 7 0.8 8 0.9 9 The comment in this example suggests that each raw or epsilon and compare numbers combinations should be on a seperate line.They aren't. Do you know why?Bill

Link to post
Share on other sites
browsers use text/html as default content typetry addingheader('Content-type: text/plain');at the top of the page (after the <?php tag
Wander,Thanks for your suggestion. It did and didn't work.It prompted a File Download critical message, but then when I accepted the download the output received was like the comment in the example said it should be.What next?Bill
Link to post
Share on other sites

thats strange, gues somehow theres some weird char tat cant be shown in plain textremove the header that i said, and replace it byecho "<pre>";

Link to post
Share on other sites
Wander,I put that line in the other .php files that weren't printing with the new line and they do now.I've gotta' study that <pre> command.Bill
The <pre> tag isn't the solution. The \n inside the echo statement sends a new line character to your html source code. Your browser ignores line breaks, and more than one space in a row, when displaying the output in the browser. It's called "whitespace" and is ignored.You use the \n and \t to make your html source look nice. Its easier to spot mistakes when you choose View Source from your browser window.As Deirdre's Dad has mentioned, if you want to force a break in the webpage you have a few options. The first is <br /> (a break) or set your text inside paragraphs <p>abc</p><p>dfe</p> etc. Or for a list you could put it in an unordered list like this:<ul><li>abc</li><li>def</li></ul>The \n and \t have no effect at all on how your webpage looks - it only changes the look of the source.As an example:echo "Hello\nThere";prints "HelloThere" on the screen, but showsHelloTherein your source code.The <pre> preserves white space in the browser. Useful for troubleshooting but probably not what you on your webpage.
Link to post
Share on other sites

I get the impression you're doing work that's more terminal oriented than browser oriented. If so, don't sweat appearances. But if this IS the foundation for some HTML project, yeah, you'll need more.

Link to post
Share on other sites

Tjhere are some fairly good 'tutorials here at w3schools.com including an excellent one in the pinned topics right here in the php sub-forum by justsomeguy re: site registration scripts.

Link to post
Share on other sites

O.K. we're getting somewhere.I am just learning PHP. I don't need to be able to create perfect programs right now.What I'm doing is reading the manual and testing the code snippets in the manual. I DO like to see vertical printing as apposed to the horizontal printing I was getting.I will look into the tutorials in the forum.Bill

Link to post
Share on other sites

Have you try this?<?phpdefine('EPSILON', 1.0e-8);function real_cmp($r1, $r2) { $diff = $r1 - $r2; if( abs($diff) < EPSILON ) return 0; else return $diff < 0 ? -1 : 1;}function real_lt($r1, $r2) { return real_cmp($r1, $r2) < 0;}echo "raw compare<br />\n";$n = 0;for($i = 0.1; $i < 1.0; $i += 0.1) { $n++; echo "$i\t$n."<br />\n";}echo "<br />\nepsilon compare<br />\n";$n = 0;for($i = 0.1; real_lt($i, 1.0); $i += 0.1) { $n++; echo "$i\t$n."<br />\n";}

Link to post
Share on other sites
How about that PHP Tutorial on the W3C site?
Unless I'm mistaken the W3C don't publish tutorials. Anyway, PHP isn't developed by them, its done by the PHP Group.
Link to post
Share on other sites

W3Schools != W3C (World Wide Web Consortium)There is no relation. Why are so many people mistaken? Must be why W3Schools is so popular :)

Link to post
Share on other sites
W3Schools != W3C (World Wide Web Consortium)There is no relation. Why are so many people mistaken? Must be why W3Schools is so popular :)
I want to change my original question from: How about that PHP Tutorial on the W3C site?to: How about the PHP Tutorial on this site?Bill
Link to post
Share on other sites

The PHP tutorials on this site are bad, in my personal opinion. When i was first learning PHP the PHP tutorials had JUST been put up because PHP had just recently gotten popular. Even back then I did not find them helpful, and as of now I find them il-explained. You'd be better off finding somewhere else to get GOOD PHP tutorials.

Link to post
Share on other sites

Ouch Jhecht, but I suppose I agree. For examples and reference maybe, but if you are learning it does not walk you through the steps like a good tutorial should.

Link to post
Share on other sites

Yeah, that IS the reference. Can't do better for clear talk about functions and groups of related functions. In fact, if you follow up that address with the name of a function, it'll take you straight to the ref page. If you follow it up with a word like "string" it'll teach you all about strings. A very smart server with mirrors all over the world.But if you're looking for a general sense of the way things are done, look at some complete scripts from a source you can trust. Just knowing something is possible doesn't make it good sense. See what people really do. Right here is a good place for that.

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...