开发者

My PHP newline beginner code isn't going to a new line. Little help?

Here's my code:

<html&开发者_运维问答gt;
    <Head>
    <?php   
    $name = "Sergio";
    ?>
    </Head>
    <body>
        <h1>It works!</h1>
        <?php 
        echo "So someone tells me your name is " . $name . ".";
        echo "Welcome to the site, " . $name . "\n";    
        echo "THEN WHO WAS NEW LINE?";
        ?>
    </body>
</html>

Everything outputs to a single line with no newline. Any help?


Use <br /> because you are outputing on the browser which needs html tags.

 echo "So someone tells me your name is " . $name . "<br />";
 echo "Welcome to the site, " . $name . "<br />";    
 echo "THEN WHO WAS NEW LINE?" . "<br />";


HTML ignores all newlines, so you'll have to use <br /> to insert a line break.


HTML isn't plain text. When you want a line break, you have to insert it with markup, such as <br>... though probably separate paragraphs would be more appropriate.

Also since HTML isn't plain text, you need to HTML-escape it when you output it. It doesn't matter for “Sergio”, but it would matter if someone's name was “Brian <i> Smith”, the guy with the unusual middle name who would turn your whole page italic if you didn't escape it properly.

<body>
    <h1>It works!</h1>
    <p>
        So someone tells me your name is <?php echo htmlspecialchars($name); ?>.
    </p>
    <p>
        Welcome to the site, <?php echo htmlspecialchars($name); ?>.
    </p>
</body>


if you nest string in pre tags then /n will work.

echo "<pre>line one\nline two</pre>";

but font will not be same as non pre'ed text


<html> 
<Head> 
<?php    
$name = "Sergio"; 
?> 
</Head> 
<body> 
    <h1>It works!</h1> 
    <?php  
    echo "So someone tells me your name is " . $name . ". "; 
    echo "Welcome to the site, " . $name . "<br/>";     
    echo "THEN WHO WAS NEW LINE?"; 
    ?> 
</body> 

I would put a breakline instead will do what you are looking for


If your not running it through a web browser you can echo or printf "\n"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜