tags:

views:

73

answers:

5

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.

+4  A: 
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.

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

You can do

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

or

echo '<div>
    <h3>stuff</h3>
</div>
';
leChuck
The second example is what I was looking for. Thanks
DrDro
A: 

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;
Álvaro G. Vicario
A: 

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

Ignacio Vazquez-Abrams
I would want to had te line returns for the readability of the html source code
DrDro
But that's what tidy is for. http://php.net/manual/en/book.tidy.php
Ignacio Vazquez-Abrams
Well, technically newlines are just unnecessary whitespace (=bandwidth), but he obviously cares about the look of the output's source code.
Alan
@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
@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.
Alan
A: 
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.

LnDCobra