tags:

views:

838

answers:

4

Here's my code:

<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 . "\n";    
        echo "THEN WHO WAS NEW LINE?";
        ?>
    </body>
</html>

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

+6  A: 

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 />";
Sarfraz
Thank you for the help however, if I use the \n on a string variable, it'll output to a newline right?
Serg
"\n" will output a newline in the source (or in files if you're going to write to files). However, "\n" is ignored by HTML, you'll need the <BR> tag in HTML to get a newline. Note also that '\n' will write the characters '\' and 'n' to the output. "\n" will write a newline. (notic the difference between single and double quotes).
Lex
Thanks guys, all the best.
Serg
@Sergio: You are welcome :)
Sarfraz
+1  A: 

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

Douwe Maan
A: 
<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

Sleepy Rhino
+1  A: 

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>
bobince