Page 1 of 1

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

Posted: Fri Apr 06, 2007 9:50 am
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]

Posted: Fri Apr 06, 2007 9:57 am
by Benjamin
In a browser? use <br /> instead

Posted: Fri Apr 06, 2007 9:58 am
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.

Posted: Fri Apr 06, 2007 10:04 am
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!

Posted: Fri Apr 06, 2007 10:31 am
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.

Posted: Fri Apr 06, 2007 10:35 am
by John Cartwright
hint: <br /> is a string, therefore it must be within quotes.

Posted: Fri Apr 06, 2007 10:43 am
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.

Posted: Fri Apr 06, 2007 10:56 am
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.

Posted: Fri Apr 06, 2007 11:27 am
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);
?>

Posted: Sat Apr 07, 2007 6:56 am
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);
?>

Posted: Sat Apr 07, 2007 11:29 am
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. :)