Javascript php脚本中的双引号echo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11036420/
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-24 04:09:28  来源:igfitidea点击:

Double quotes within php script echo

javascriptphpsyntaxecho

提问by RXC

I have a line of php code that looks like this:

我有一行 php 代码,如下所示:

echo "<script>$('#edit_errors').html('<h3><em>Please Correct Errors Before Proceeding</em></h3>')</script>";

I would like to know how to add a font color to the text correctly. If I do this:

我想知道如何正确地为文本添加字体颜色。如果我这样做:

echo "<script>$('#edit_errors').html('<h3><em><font color="red">Please Correct Errors Before Proceeding</font></em></h3>')</script>";

The word "red" is in black text and the compiler throws an error.

“红色”一词是黑色文本,编译器会抛出错误。

If I use single quotes around red, then the text does not show up at all.

如果我在红色周围使用单引号,则文本根本不会显示。

Any help would be great. Thanks

任何帮助都会很棒。谢谢

回答by Zbigniew

You need to escape ", so it won't be interpreted as end of string. Use \to escape it:

你需要转义",所以它不会被解释为字符串的结尾。使用\逃脱它:

echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";

Read more: stringsand escape sequences

阅读更多:字符串转义序列

回答by Marc B

use a HEREDOC, which eliminates any need to swap quote types and/or escape them:

使用HEREDOC,它消除了交换引用类型和/或转义它们的任何需要:

echo <<<EOL
<script>$('#edit_errors').html('<h3><em><font color="red">Please Correct Errors Before Proceeding</font></em></h3>')</script>
EOL;

回答by John Conde

Just escape your quotes:

只是逃避你的报价:

echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";

回答by Ryan

You need to escape the quotes in the string by adding a backslash \before ".

您需要通过\".之前添加反斜杠来转义字符串中的引号。

Like:

喜欢:

"<font color=\"red\">"

回答by mark

if you need to access your variables for an echo statement within your quotes put your variable inside curly brackets

如果您需要访问引号内的 echo 语句的变量,请将变量放在大括号内

echo "i need to open my lock with its: {$array['key']}";

回答by mario

You can just forgo the quotes for alphanumeric attributes:

您可以放弃字母数字属性的引号:

echo "<font color=red> XHTML is not a thing anymore. </font>";
echo "<div class=editorial-note> There, I said it. </div>";

Is perfectly valid in HTML, and though still shunned, absolutely en vogue since HTML5.

在 HTML 中完全有效,尽管仍然被回避,但自 HTML5 以来绝对流行。

CAVEATS

警告