PDA

View Full Version : Very simple PHP prob?



ashleysnix
12th September 2004, 01:26 AM
Hi

Just starting to learn a little PHP by doing exercises from the O'Reilly book Learning PHP 5. It's been a long time since I've looked at code so please bare with me!

I can't understand why the following code does not come out formatted correctly in both firefox & ie. It is supposed to output the results in a vertically aligned format, but instead comes out in 1 long line:

2 Hamburger at $4.95 each: $9.90 1 Milkshake at $1.95 each: $1.95 1 Cola at $0.85 each: $0.85 Food & Drink Total: $12.70

For some reason the "\n" newline special charachter does not seem to work. Herte is the code:


<?php
$hamburger = 4.95;
$milkshake = 1.95;
$coke = 0.85;
$food = 2 * $hamburger + $milkshake + $coke;
$sales_tax = $food * 0.075;
$tax_tip = $food * 0.16;

printf("%1d %9s at \$%.2f each: \$%.2f\n", 2, 'Hamburger', $hamburger, 2 * $hamburger);
printf("%1d %9s at \$%.2f each: \$%.2f\n", 1, 'Milkshake', $milkshake, $milkshake
);
printf("%1d %9s at \$%.2f each: \$%.2f\n", 1, 'Cola', $coke, $coke);
printf("%25s: \$%.2f\n", 'Food & Drink Total', $food);`
printf("%25s: \$%.2f\n", 'Total with Tax', $food + $sales_tax);
printf("%25s: \$%.2f\n", 'Total with Tax & Tip', $food + $sales_tax + $tax_tip);`
?>

Am i totally missing something?

Thanks

Ashley

i1n2d3y4
12th September 2004, 01:38 AM
use the html <br> tag instead of the C style \n to get new lines. I am not sure if you can use the C style tags to do HTML using php, there might be a way, but I try and stick to outputting HTML to ensure that the style stays consistent.

Hope this helps

ashleysnix
12th September 2004, 02:09 AM
Sure did help, thanks...

In fact your answer made me click. These questions are made for the cli, not the web.

pigpen
12th September 2004, 06:17 PM
'\n' will create line breaks in your html sourcecode. also very useful at times...

Shigun
30th September 2004, 07:18 PM
'\n' will create line breaks in your html sourcecode. also very useful at times...

'\n' will create line breaks in PHP output, not in HTML based code. For HTML based code, you have to use the HTML tag <br>.

pigpen
30th September 2004, 07:43 PM
'\n' will create line breaks in PHP output, not in HTML based code. For HTML based code, you have to use the HTML tag <br>.
That's why I said HTML sourcecode.