How to work with double quotes and single quotes i

2020-02-07 12:43发布

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'";

标签: php echo
2条回答
霸刀☆藐视天下
2楼-- · 2020-02-07 13:24

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

查看更多
混吃等死
3楼-- · 2020-02-07 13:29

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'";
查看更多
登录 后发表回答