tags:

views:

65

answers:

3

I'm just starting PHP programming as you can tell. I want the results of each statement to be on their own line but can't get it to work yet. I tried referring to the code on this page and am obvsiously doing something wrong. Thank you very much.

<?
$mySentence="This is a sentence 123456789.";
$myNumber1 = 9.5;
$myNumber2 = .5;
$sum = $myNumber1 + $myNumber2;

echo "Hello, lets display our PHP variables: \r";
echo $mySentence;
echo "The sum of $myNumber 1 and $myNumber2 = $sum ";
echo "\"This text has double quotes\"";

?>
A: 

Try using \n instead of \r on the first line and in the rest you'll have to add \n at the end as:

echo "Hello, lets display our PHP variables: \n";
echo $mySentence."\n";
echo "The sum of $myNumber 1 and $myNumber2 = $sum\n"
echo "\"This text has double quotes\"\n";

If you want these to be echoed in a HTML page on separate lines you'll have to use the line break tag <br /> in place of \n

codaddict
That and use the extended `<?php` tag instead of the short often not supported `<?` version
kemp
Thanks, I can add in a <br> tag and it works in my string variable but the other variables aren't returning a new line when I'm using "\n". If I take the last line out it all returns on the same line and if I leave the last line in I get "Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' in /var/www/test/index.php on line 10". Thank you
Jeremy Person
What server are you running on? Check out my summary in my answer.
Anthony Forloney
Thanks again, I'm running PHP Version 5.2.10-2ubuntu6.4 locally and I've also tried uploading it to my GoDaddy web server running PHP Version 5.2.5 (http://jeremyperson.com/phpinfo.php). For some reason it is still rendering in one long string and it isn't performing a carriage return. Server output - http://jeremyperson.com/php/
Jeremy Person
Looks like I can use HTML.<?php$mySentence="This is a sentence blah, blah, blah.";$myNumber1 = 9.5;$myNumber2 = .5;$sum = $myNumber1 + $myNumber2;$today=date("d-m-Y");$time=date("H:i:s");$browser=$HTTP_USER_AGENT;$name="Jeremy Person";echo ("<b>Name:</b> $name <br>");echo ("<b>Time:</b> $time <br>");echo ("<b>Date:</b> $today <br>");echo ("<b>Browser:</b> $browser <br><br>");echo "<b>My PHP Variables:</b> <br>";echo "<b>My Sentence:</b> $mySentence<br>";echo "<b>The sum of $myNumber1 and $myNumber2:</b> $sum"?>
Jeremy Person
+6  A: 
"\n"

is ascii new line code

if you want to see the line break on html you should use

<br />

like this:

<?php

echo "Hello world! <br />";
echo "I'm Gabriel";
?>
Gabriel Sosa
It should be pointed out more explicitly to the OP that when html formated documents are displayed the web client ignores traditional ascii formatting characters and that sequences of white space characters on the same line get compressed into a single space. So if you output "Hello\tWorld ! !" it will display as "Hello World ! !"
Robert S. Barnes
A: 

Use HTTP header to do this.

header("Content-Type: text/plain");

This allows plain text to be displayed correctly.

Also, echo the <pre> tag or use the PHP function nl2br, they both work.

For example:

<?php echo "<pre>line 1\nline 2</pre>"; ?>

or

<?php echo nl2br("line 1\nline 2"); ?>
SHiNKiROU