Question about \n - Does not seem to work - Please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stinkypinky
Forum Newbie
Posts: 3
Joined: Fri Apr 06, 2007 9:45 am

Question about \n - Does not seem to work - Please help

Post by stinkypinky »

Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I JUST started learning php and have a very simple question.  I honestly tried to look around to find an answer, but can't seem to find one.

Like I said, I am a 100% beginner - so please excuse this simple question!

I am writing a short little code, and all I want is to add line breaks after a printf command.  My code looks like this:

Code: Select all

<?php 
$ham = 4.95;
$shake = 1.95;
$cola = .85;
$food = 2 * $ham + $shake + $cola; 
$tax = $food *.075;
$tip = $food *.16;
$total = $food + $tip + $tax;
printf("%1d %3s at \$%.2f each: \$%.2f\n", 2, 'ham', $ham, 2 * $ham); 
printf ("%1d %5s at \$%.2f each: \$%.2f\n", 2, 'shake', $shake, 2 * $shake);
?>
A line break is not showing up after each "printf" and it appears as one long continuous string....why isn't the \n working?

Thank very very much in advance for anyone that can help me!


Everah | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

In a browser? use <br /> instead
bubblenut
Forum Newbie
Posts: 20
Joined: Sat Feb 03, 2007 4:16 am
Location: London

Post by bubblenut »

I am assuming you're running this as a web page, in which case a line break is represented by the html element <br />
Try adding a <br /> tag before the newline characters.
stinkypinky
Forum Newbie
Posts: 3
Joined: Fri Apr 06, 2007 9:45 am

Post by stinkypinky »

Thank you so much. I am in a browser, but where exactly do I put the <br />? Is it where the \n is now or after the semi-colon? I tried it after the semi-colon and it doesn't seem to work. Can someone show me where exactly to put the <br />?

Thank you very much!
bubblenut
Forum Newbie
Posts: 20
Joined: Sat Feb 03, 2007 4:16 am
Location: London

Post by bubblenut »

Don't ask to be spoon-fed, you're not a baby. You've been given plenty of information for you to solve the problem youself, try a few ways and you will find the answer.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

hint: <br /> is a string, therefore it must be within quotes.
stinkypinky
Forum Newbie
Posts: 3
Joined: Fri Apr 06, 2007 9:45 am

Post by stinkypinky »

Thank you jcart. And bubblenut - i HAD tried it a few ways before i made the second post and nothing was working....nothing....try not to be so mean. this is the last time I'll come to this forum anyway since some people seem to be rude. but thanks to the people who helped.
bubblenut
Forum Newbie
Posts: 20
Joined: Sat Feb 03, 2007 4:16 am
Location: London

Post by bubblenut »

I'm sorry, that was a bit mean of me. I was trying to encourage you to spend some time solving the problem yourself as the answers stick in the head better when you discover them yourself.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I hope you don't decide to leave. Try not to let one rude person scare you away, we are a great community of helpful people.

Code: Select all

<?php
$ham = 4.95;
$shake = 1.95;
$cola = .85;
$food = 2 * $ham + $shake + $cola;
$tax = $food *.075;
$tip = $food *.16;
$total = $food + $tip + $tax;
printf("%1d %3s at \$%.2f each: \$%.2f\n", 2, 'ham', $ham, 2 * $ham);
echo '<br />'; // You can add it here.
printf ("%1d %5s at \$%.2f each: \$%.2f\n", 2, 'shake', $shake, 2 * $shake);
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Everah wrote://you can add it here

Code: Select all

<?php
$ham = 4.95;
$shake = 1.95;
$cola = .85;
$food = 2 * $ham + $shake + $cola;
$tax = $food *.075;
$tip = $food *.16;
$total = $food + $tip + $tax;
printf("%1d %3s at \$%.2f each: \$%.2f<br />", 2, 'ham', $ham, 2 * $ham); //or here
printf ("%1d %5s at \$%.2f each: \$%.2f<br />", 2, 'shake', $shake, 2 * $shake);
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

True that. I actually had started to do that, but thought for readability it would be easier to spot put between the two. But I like yours more. :)
Post Reply