PHP脚本中的双引号echo
我有一行php代码,看起来像这样:
echo "<script>$('#edit_errors').html('<h3><em>Please Correct Errors Before Proceeding</em></h3>')</script>"; 我想知道如何正确添加字体颜色的文字。 如果我这样做:
 echo "<script>$('#edit_errors').html('<h3><em><font color="red">Please Correct Errors Before Proceeding</font></em></h3>')</script>"; 
“红色”一词是黑色文字,编译器会报错。
如果我用红色的单引号,那么文本根本不显示。
任何帮助将是伟大的。 谢谢
 你需要转义" ,所以它不会被解释为string的结尾,用\来转义它: 
 echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>"; 
阅读更多: string和转义序列
使用HEREDOC ,这消除了交换报价types和/或逃避它们的任何需要:
 echo <<<EOL <script>$('#edit_errors').html('<h3><em><font color="red">Please Correct Errors Before Proceeding</font></em></h3>')</script> EOL; 
只是逃避你的报价:
 echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>"; 
 您需要通过在"之前添加反斜杠\ "来避免string中的引号。 
喜欢:
 "<font color=\"red\">" 
如果你需要访问你的variables在你的引号内的echo语句,把你的variables放在大括号内
 echo "i need to open my lock with its: {$array['key']}";