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
The exception for single-quoted strings is a single quote (and backslash when needed). If you were to echo this string in PHP: 'Sammy says: "This string\'s in single quotes." It required a backslash (\) before the apostrophes (\\\'), but do not use (\") with the double quotes.
Double-quoted strings: By using Double quotes the PHP code is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences.
Just escape them: echo "\"$time\""; You could also use single around the double quotes: echo '"' .
In PHP, people use single quote to define a constant string, like 'a' , 'my name' , 'abc xyz' , while using double quote to define a string contain identifier like "a $b $c $d" . echo "my $a"; This is true for other used of string.
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
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;
Just escape your quotes:
echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";
You need to escape the quotes in the string by adding a backslash \
before "
.
Like:
"<font color=\"red\">"
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']}";
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
htmlspecialchars
or some whitelisting).<font>
specifically is somewhat outdated however.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With