如何在 PHP、html 标签中回显

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3931351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:29:35  来源:igfitidea点击:

How to echo in PHP, html tags

phphtmltagsecho

提问by Trufa

I went through this before posting:

我在发帖之前经历了这个:

How can I echo HTML in PHP?

如何在 PHP 中回显 HTML?

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?

但是我仍然找不到使标签“”和“”消失的方法,我该怎么办?

回答by William Macdonald

<?php

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

?>

Just put it in single quotes.

只需将其放在单引号中即可。

回答by codaddict

Did you try the heredocbased solution:

您是否尝试过基于heredoc的解决方案:

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

回答by Ignacio Vazquez-Abrams

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
  ...
?>

回答by Saladin Akara

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.

您需要对 进行转义,"以便 PHP 不会将它们识别为您的 PHP 代码的一部分。您可以通过使用\转义字符来完成此操作。

So, your code would look like this:

因此,您的代码将如下所示:

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

回答by Alin Purcaru

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

如果你想输出大量的 HTML,你应该考虑使用heredocnowdoc语法。这将使您无需转义即可编写字符串。

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

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

另请注意,由于 PHP 是一种嵌入式语言,因此您可以在 HTML 内容之间添加它,而无需回显任何标签。

<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; ?>.

此外,如果您只想输出一个变量,您应该使用简写输出标签<?=$var?>。这相当于<?php echo $var; ?>.

回答by Wazy

Separating HTML from PHP is the best method. It's less confusing and easy to debug.

将 HTML 与 PHP 分开是最好的方法。它不那么令人困惑且易于调试。

<?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++;
  }
?>

回答by Victor Nicollet

You have a variety of options. One would be to use PHP as the template engine it is:

您有多种选择。一种是使用 PHP 作为模板引擎,它是:

<?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:

另一种方法是使用 HEREDOC,它保留双引号不变,支持换行符,并扩展其中的任何变量:

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

回答by Dave Niaga Lape

no need to use echo sir just use the tag . welcome :)

无需使用 echo sir 只需使用标签即可。欢迎 :)

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

回答by platkos

You can replace '<' with &lt;and '>' with &gt;for ex:

您可以将“<”替换为&lt;“>”,&gt;例如:

echo "&lt;div&gt;";

output will be visible <div>

输出将可见 <div>

for longer strings make a function, for ex

对于更长的字符串创建一个函数,例如

function example($input) {
    $output = str_replace('>', '&gt;', str_replace('<', '&lt;', $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

不要忘记放置反斜杠href=\"#\"或使用单引号进行操作href='#'或使用 str_replace 在函数中更改它

回答by Anshu Aditya

This will also work fine with double quotes. To echoany html_tag with double quotes we just need to remember one thing, DO NOT USE ANY OTHER DOUBLE QUOTES(") IN THE MIDDLE.

这也适用于双引号。要使用双引号回显任何 html_tag,我们只需要记住一件事,不要在中间使用任何其他双引号 (")。

    <?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 echois enclosed within the single quotes. This is the precaution you should take while using the double quotes for this purpose.

注意这里 php echo 中的链接用单引号括起来。这是为此目的使用双引号时应采取的预防措施。