22

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

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

Gui Imamura
  • 556
  • 9
  • 26
RXC
  • 1,233
  • 5
  • 36
  • 67

6 Answers6

64

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: strings and escape sequences

Zbigniew
  • 27,184
  • 6
  • 59
  • 66
13

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

echo <<<EOL
<script>$('#edit_errors').html('<h3><em><font color="red">Please Correct Errors Before Proceeding</font></em></h3>')</script>
EOL;
Marc B
  • 356,200
  • 43
  • 426
  • 500
4

Just escape your quotes:

echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";
John Conde
  • 217,595
  • 99
  • 455
  • 496
3

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

Like:

"<font color=\"red\">"
Ryan
  • 1,797
  • 12
  • 19
  • how to write < or > characters. Since writing them changes to < and > respectively :( – Aada Apr 23 '14 at 03:14
2

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

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

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.

CAVEATS

mario
  • 144,265
  • 20
  • 237
  • 291