20

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...

jww
  • 97,681
  • 90
  • 411
  • 885
Nasir Zia
  • 564
  • 1
  • 6
  • 20
  • 8
    `echo 'Here goes your message with an apostrophe S like thi\'s ';` – k102 Aug 03 '12 at 09:29
  • 2
    +1 K102. You can read [that](http://php.net/manual/fr/function.echo.php) and, you want using doubleQuote in echo function : echo " it's me!" ; – Doc Roms Aug 03 '12 at 09:32

7 Answers7

37

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";
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
5

Escape the quote using a backslash.

'hello\'s'

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

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
He Hui
  • 2,196
  • 4
  • 29
  • 46
5

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.

john k
  • 6,268
  • 4
  • 55
  • 59
2

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 ";
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Jalpesh Patel
  • 3,150
  • 10
  • 44
  • 68
2
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.

Prasanth
  • 5,230
  • 2
  • 29
  • 61
0

Since the methods from the answers didn't work for my situation I ended up just calling a new echo everytime the type of quote changed through the code and swapped the type of quote to start the echo, it's 2019 now and I don't know about any other solution since I am really new to programming but this worked fine for me, example:

        else {
          echo '<a onclick="document.getElementById(';
          echo "'open_login').style.display='block'";
          echo '" class="branding w3-bar-item w3-button w3-mobile w3-light-blue w3-hover-white w3-right"href="#login"><span class="fa fa-user"></span>&nbsp;&nbsp;Login do Aluno</a>';
        }
-1

The problem may be that you are passing the variable through HTML in single quotes ''. If it is the case, try using double quotes: "".

  • Welcome to StackOverflow. Please review existing answers to the questions. Try not to duplicate those, especially on questions that are 8 years old. – thelr May 15 '20 at 18:34