How to use single quote inside an echo which is using single quote

First of all, i have gone through the related questions.. haven't found any answer.. I m using this code to display a message

echo 'Here goes your message with an apostrophe S like thi's ';

How can i make this work, as any quote inside this echo will break the statement...


Either escape the quote with a backslash, or use double quotes to designate the string.

echo 'Here goes your message with an apostrophe S like thi\'s';

echo "Here goes your message with an apostrophe S like thi's";

Escape the quote using a backslash.

'hello\'s'

The single quote that appears after the backslash will appear on screen.


Have you tried the function addslashes()? It even uses your example.

I personally prefer the function htmlspecialchars() which does the same thing but has flags which let you specify its behavior.

like so:

echo htmlspecialchars("O'Rielly", ENT_QUOTES);

This shows the string properly on an HTML webpage.


echo <<<EOT
You can put what ever you want here.. HTML, " ' ` anyting will go
Here goes your message with an apostrophe S like thi's
EOT;

Be sure to read this before using such kind of strings.


In PHP, the escape sequence character is the backslash (\). You can add this before special characters to ensure that those characters are displayed as literals. For example:

echo 'Here goes your message with an apostrophe S like thi\'s ';

Or you can also write like this:

echo "Here goes your message with an apostrophe S like thi's ";