如何设置 php echo 输出的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7147609/
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
how to style php echo output
提问by culter
It's probably stupid question, but I can not find an answer. How can I style echo output with css? I have this code:
这可能是一个愚蠢的问题,但我找不到答案。如何使用 css 设置 echo 输出的样式?我有这个代码:
echo "<div id="errormsg"> Error </div>";
Now it displays syntax error, I think because of those quotes around errormsg. I've tried single quotes, but with no effect. Thank you
现在它显示语法错误,我认为是因为 errormsg 周围的那些引号。我试过单引号,但没有效果。谢谢
回答by Dunhamzzz
When outputting HTML, it's easier to use single quotes so you can use proper double quotes inside like so:
输出 HTML 时,使用单引号更容易,因此您可以在其中使用正确的双引号,如下所示:
echo '<div id="errormsg"> Error </div>';
That will get rid of your parse error... To edit the style you will need to use CSS with the selector of #errormsg
like so:
这将消除您的解析错误...要编辑样式,您需要使用带有如下选择器的 CSS #errormsg
:
#errormsg {
color: red;
}
回答by Napas
try
尝试
echo "<div id=\"errormsg\"> Error </div>";
回答by David says reinstate Monica
First you need to either use single-quotes to surround the attribute value:
首先,您需要使用单引号将属性值括起来:
echo "<div id='errormsg'> Error </div>";
Or you could reverse that, to give:
或者你可以扭转它,给出:
echo '<div id="errormsg"> Error </div>';
Or you should escape the quotes:
或者你应该转义引号:
echo "<div id=\"errormsg\"> Error </div>";
And then style the resulting element with the CSS:
然后使用 CSS 设置结果元素的样式:
#errormsg {
/* css */
}
The syntax problem you were encountering is a result of terminating the string and then having a disparate element between the first and second strings, with which PHP has no idea what to do.
您遇到的语法问题是终止字符串然后在第一个和第二个字符串之间有一个不同的元素的结果,PHP 不知道该怎么做。
回答by Facebook Staff are Complicit
To put double quotes inside of a double-quoted string, you need to "escape" them by putting blackslashes before them:
要将双引号放在双引号字符串中,您需要通过在它们之前放置黑斜杠来“转义”它们:
echo "<div id=\"errormsg\"> Error </div>";
In this case, another choice is to use single quotes for one or the other.
在这种情况下,另一种选择是对一个或另一个使用单引号。
echo "<div id='errormsg'> Error </div>";
echo '<div id="errormsg"> Error </div>';
PHP's documentation has a section explaining the different string syntaxes, which should explain everything you could want to know about this subject.
PHP 的文档有一个部分解释了不同的字符串语法,它应该解释了您可能想了解的有关该主题的所有内容。
回答by Quentin
You are getting a syntax error because you are including unescaped double quotes inside a string that is delimited by double quotes.
您收到语法错误,因为您在由双引号分隔的字符串中包含未转义的双引号。
Either escape them
要么逃避他们
echo "<div id=\"errormsg\"> Error </div>";
or use single quotes
或使用单引号
echo '<div id="errormsg"> Error </div>';
The browser doesn't care if you generated markup using echo
or something else. It just sees the HTML you send to it.
浏览器不关心您是否使用echo
或其他方式生成标记。它只会看到您发送给它的 HTML。
For the above markup, you can style it using an id selector:
对于上述标记,您可以使用 id 选择器对其进行样式设置:
#errormsg { /* … */ }
The usual rules for the cascade (including specificity) will apply.
级联的通常规则(包括特异性)将适用。
回答by Michael Berkowski
Use single quotes around errormsg
and what you have should work just fine. Alternatively, but less tidy, you can escape the double quotes with a backslash.
使用单引号errormsg
,你所拥有的应该可以正常工作。或者,但不太整洁,您可以使用反斜杠转义双引号。
echo "<div id='errormsg'> Error </div>";
回答by Abhishek Kamal
If you don't want to care about single quotes or double quotesthen the better way to achieve your answer is to use heredoc syntax.
Your solution :
如果您不想关心单引号或双引号,那么获得答案的更好方法是使用heredoc 语法。
您的解决方案:
<?php
$heredoc = <<< EOT
<div id="errormsg">Error solved</div>
EOT;
echo "$heredoc";
?>
css : #errormsg{color: green;}
WARNING :
CSS:#errormsg{color: green;}
警告:
- Do not add whiteSpace after
<<< EOT
- Do not add whiteSpace before
EOT
; - Do not add whiteSpace between
EOT
and;
- Do not add whiteSpace after
EOT;
EOT;
must be in new line.
- 后面不要加空格
<<< EOT
- 前不要加空格
EOT
; - 不要在
EOT
和之间添加空格;
- 后面不要加空格
EOT;
EOT;
必须在新行。