Reputation: 40747
I went through this before posting:
And I 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?
Upvotes: 14
Views: 148571
Reputation: 47
Here I have added code, the way you want line by line.
The .=
helps you to echo multiple lines of code.
$html = '<div>';
$html .= '<h3><a href="#">First</a></h3>';
$html .= '<div>Lorem ipsum dolor sit amet.</div>';
$html .= '</div>';
$html .= '<div>';
echo $html;
Upvotes: 0
Reputation: 15
This will also work fine with double quotes. To echo any html_tag with double quotes we just need to remember one thing, Do not use any other double quotes(") in the middle.
<?php
echo "
<div>
<h3><a href='https://stackoverflow.com/questions/3931351/how-to-echo-in-php-html-tags'>First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<div>";
?>
Notice here the link inside the PHP echo is enclosed within the single quotes. This is the precaution you should take while using the double quotes for this purpose.
Upvotes: 0
Reputation: 155
You can replace '<' with <
and '>' with >
. For example:
echo "<div>";
The output will be visible <div>
.
For longer strings, make a function, for example
function example($input) {
$output = str_replace('>', '>', str_replace('<', '<', $html));
return $output;
}
echo example($your_html);
Don't forget to put backslashes href=\"#\"
or do it with single quotes href='#'
or change it in a function too with str_replace.
Upvotes: 1
Reputation: 37
There isn't any need to use echo, sir. Just use the tag <plaintext>:
<plaintext>
<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
Upvotes: 0
Reputation: 455460
Try the heredoc-based solution:
echo <<<HTML
<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<div>
HTML;
Upvotes: 12
Reputation: 8886
Separating HTML from PHP is the best method. It's less confusing and easy to debug.
<?php
while($var)
{
?>
<div>
<h3><a href="User<?php echo $i;?>"><?php echo $i;?></a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<?php
$i++;
}
?>
Upvotes: 4
Reputation: 44386
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; ?>
.
Upvotes: 4
Reputation: 24587
You have a variety of options. One would be to use PHP as the template engine it is:
<?php
// Draw the page
?>
<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<?php
// Done drawing.
?>
Another would be to use single quotes, which let you leave double quotes unquoted and also support newlines in literals:
<?php
echo '<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>';
?>
Another would be to use a HEREDOC, which leaves double quotes untouched, supports newlines, and also expands any variables inside:
<?php
echo <<<EOS
<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
EOS;
?>
Upvotes: 2
Reputation: 2548
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>"
Upvotes: 5
Reputation: 799560
Using the first mechanism given there will do it.
<?php
...
?>
<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<div>
<?php
...
?>
Upvotes: 8
Reputation: 2167
<?php
echo '<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<div>';
?>
Just put it in single quotes.
Upvotes: 25