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?
<?php
echo '<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<div>';
?>
Just put it in single quotes.
Try the heredoc -based solution:
echo <<<HTML
<div>
<h3><a href="#">First</a></h3>
<div>Lorem ipsum dolor sit amet.</div>
</div>
<div>
HTML;
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
...
?>
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>"
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; ?>
<?php echo $var; ?>
.
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++;
}
?>
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;
?>
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.
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>
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.
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.