Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double quotes within php script echo

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

like image 755
RXC Avatar asked Jun 14 '12 15:06

RXC


People also ask

How do you echo quotes in PHP?

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.

Can you use double quotes in PHP?

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.

How can I add double quotes to a variable in PHP?

Just escape them: echo "\"$time\""; You could also use single around the double quotes: echo '"' .

What is the difference between using double quote single quote with echo command in PHP?

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.


6 Answers

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

like image 157
Zbigniew Avatar answered Oct 05 '22 15:10

Zbigniew


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;
like image 32
Marc B Avatar answered Oct 05 '22 16:10

Marc B


Just escape your quotes:

echo "<script>$('#edit_errors').html('<h3><em><font color=\"red\">Please Correct Errors Before Proceeding</font></em></h3>')</script>";
like image 29
John Conde Avatar answered Oct 05 '22 16:10

John Conde


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

Like:

"<font color=\"red\">"
like image 36
Ryan Avatar answered Oct 05 '22 15:10

Ryan


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']}";
like image 34
mark Avatar answered Oct 05 '22 16:10

mark


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

  • It's only valid for mostly alphanumeric and dash combinations.
  • Never ever do this with user input appended to attributes (those need quoting and htmlspecialchars or some whitelisting).
  • See also: When the attribute value can remain unquoted in HTML5
  • In other news: <font> specifically is somewhat outdated however.
like image 35
mario Avatar answered Oct 05 '22 17:10

mario