We can use single quotes as well as double quotes for specifying attributes for HTML tags.
i-e We can use as below,
or we can use as below also,
i-e Both alert("Hi") and alert('Hi') are valid.
But we need to take care when using them.
Consider below scenario.
We are having set of images stored in a folder and we are having image names in database.
Assume that our requirement is to display image based on the image name when clicking a link "Click here to Display Image".
<a href="#" onClick="displayName('<?php echo $name; ?>');">Click here to Display Image</a>
Assume that the $name is having single quotes as below
In this case, we will get syntax error from browser because of this single quotes.
So, to avoid this syntax error we can escape the single quotes using escape character as below.
It will avoid syntax error. But, still the image may not display correctly.
We have tried to escape it as similar to previous one using below code.
But, it didn't work correctly. Because single quotes were unexpectedly replaced with question mark symbol (?).
We have tried to use the HTML Entity ". But it didn't work.
Finally, the entity "'" worked correctly to display the images having single quotes in their name.
After making these changes everything worked fine.
Similarly, we may face issues for inserting data into database table if the data is having single quotes.
Normally for inserting string with single quotes, we can replace the single quotes in the string with two single quotes before inserting them into the database.