1

I just wonder how people work with double and single quotes. I have a code where i got a little problem.

If i for example got this code:

<img src="img_fjords.jpg" onclick="document.getElementById('someID').style.display='block'" class="w3-hover-opacity">

Now i got no problem, but if im going to put this inside a echo "" then i start getting problems, so i changed all the double quotes to single quotes and used double quotes for the echo, but still it will not accept it, i think i got to many single quotes now. and i think i need to do something different with "someID"?

echo code:

echo "<img src='img_fjords.jpg' onclick='document.getElementById('someID').style.display='block'' class='w3-hover-opacity'";
Mathias Hermansen
  • 261
  • 1
  • 5
  • 13
  • 1
    Start reading please http://php.net/manual/en/language.types.string.php#language.types.string.syntax – u_mulder Mar 14 '17 at 20:39

2 Answers2

2

You have to escape double quotes if your string is put in double quotes by using backslash \ character. Here is an example:

echo "String with \"double quotes\"";

So in your case it will be:

echo "<img src=\"img_fjords.jpg\" onclick=\"document.getElementById('someID').style.display='block'\" class=\"w3-hover-opacity\"";

Ref: Double quotes within php script echo

Community
  • 1
  • 1
Lkopo
  • 4,798
  • 8
  • 35
  • 60
2

You can escape the quotes where needed (using backslashes before the quote signs):

echo "<img src='img_fjords.jpg' onclick='document.getElementById(\"someID\").style.display=\"block\"' class='w3-hover-opacity'";
Johannes
  • 64,305
  • 18
  • 73
  • 130