开发者

PHP New Line Help

开发者 https://www.devze.com 2022-12-23 11:48 出处:网络
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 obvs开发

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 obvs开发者_StackOverflowiously 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\"";

?>


"\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";
?>


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"); ?>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消