0

How do you implement line returns when the html code is between simple quotes. The only way I found is to concatenate the line return between double quotes:

echo '<div>'."\n"
    .'<h3>stuff</h3>'."\n" 
    .'</div>'."\n";

Which lucks ugly to me.

Edit: The code between the simple quotes is quite long and with many attributes otherwise I would just use double quotes.

2
  • Is there a reason to echo? Can't you just get out of php, spit out what you need and then back into php? Commented Feb 25, 2010 at 8:45
  • It's a portion of code that will be repeated many times Commented Feb 25, 2010 at 8:58

5 Answers 5

4
echo "<div>\n" .
    "<h3>stuff</h3>\n" .
    "</div>\n";

or

echo "<div>
    <h3>stuff</h3>
    </div>\n";

or

echo <<< HTML
    <div>
        <h3>stuff</h3>
    </div>
HTML;

But this is completely subjective.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, where can I find info about that last notation. What is it called ?
@DrDro It's the Heredoc notation: php.net/manual/en/language.types.string.php -- it's a bit ugly IMO, but it avoids the quotation escaping problem completely.
2

You can do

echo "<div>\n<h3>stuff</h3>\n</div>\n";

or

echo '<div>
    <h3>stuff</h3>
</div>
';

1 Comment

The second example is what I was looking for. Thanks
0

Concatenation: echo '<div>' . "n";

Concatenation with constant: echo '<div>' . PHP_EOL;

Hard line feeds:

echo '<div>
';

End PHP mode: echo '<div>'; ?>

Double quotes: echo "<div>\n";

Heredoc:

echo <<<EOM
    <div>

EOM;

Comments

0

Single quotes inhibit backslash sequences. But newlines are pretty much optional outside of CDATA sections, so feel free to omit them.

4 Comments

I would want to had te line returns for the readability of the html source code
Well, technically newlines are just unnecessary whitespace (=bandwidth), but he obviously cares about the look of the output's source code.
@Alan I'm not against optimisation at all but my final page weights about 48k with empty cache so I'm not to worried about a few more whitespaces. I agree adding line returns in the html is arguable but that wasn't the question.
@DrDro I didn't say it's a bad thing. I like properly formatted source code as much as the next guy. I was just pointing out that it wasn't just out of standards compliance you wanted those newlines there.
0
echo <<<EOM
<div>
<h3 class="blabla" style='booboo' >stuff<h3>
</div>

EOM;

Using the <<< quotation, you don't need to escape any characters(they are simply outputted exactly how they are typed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.