How to echo in PHP, html tags

2020-01-26 10:18发布

I went through this before posting:

How can I echo HTML in PHP?

And still couldn't make it work.

I'm trying to echo this:

<div>
 <h3><a href="#">First</a></h3>
 <div>Lorem ipsum dolor sit amet.</div>
</div>
<div>

But I still can't find a way to make the tags "" and '' disappear, what do I have to do?

标签: php html tags echo
11条回答
霸刀☆藐视天下
2楼-- · 2020-01-26 11:13
<?php

echo '<div>
 <h3><a href="#">First</a></h3>
 <div>Lorem ipsum dolor sit amet.</div>
</div>
<div>';

?>

Just put it in single quotes.

查看更多
爷的心禁止访问
3楼-- · 2020-01-26 11:13

Did you try the heredoc based solution:

echo <<<HTML
<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<div>
HTML;
查看更多
不美不萌又怎样
4楼-- · 2020-01-26 11:15

You need to escape the " so that PHP doesn't recognise them as part of your PHP code. You do this by using the \ escape character.

So, your code would look like this:

echo
    "<div>
        <h3><a href=\"#\">First</a></h3>
        <div>Lorem ipsum dolor sit amet.</div>
    </div>
    <div>"
查看更多
够拽才男人
5楼-- · 2020-01-26 11:15

If you want to output large quantities of HTML you should consider using heredoc or nowdoc syntax. This will allow you to write your strings without the need for escaping.

echo <<<EOD
You can put "s and 's here if you like.
EOD;

Also note that because PHP is an embedded language you can add it between you HTML content and you don't need to echo any tags.

<div>
    <p>No PHP here!</p>
    <?php
    $name = "Marcel";
    echo "<p>Hello $name!</p>";
    ?>
</div>

Also if you just want to output a variable you should use the short-hand output tags <?=$var?>. This is equivalent to <?php echo $var; ?>.

查看更多
▲ chillily
6楼-- · 2020-01-26 11:16

Here i have added code, the way you want line by line. The back tick helps you to echo multiple line code.

$html = '<div>';
$html .= '<h3><a href="#">First</a></h3>';
$html .= '<div>Lorem ipsum dolor sit amet.</div>';
$html .= '</div>';
$html .= '<div>';
echo $html;
查看更多
登录 后发表回答