-5

I have created a PHP variable:

$msg_gifted='<center><h1>It's a gift! </h1>';

If I leave it like this the code is breaking.

When I replace the apostrophe with:

$msg_gifted='<center><h1>It is a gift! </h1>';

everything works fine. Is there a known way to solve this?

xbass540
  • 319
  • 1
  • 3
  • 14
  • [The Great Escapism (Or: What You Need To Know To Work With Text Within Text)](http://kunststube.net/escapism/) – deceze Oct 12 '17 at 10:08
  • 1
    Basic, *basic* syntax: http://php.net/manual/en/language.types.string.php – deceze Oct 12 '17 at 10:09

2 Answers2

1

you can do

$msg_gifted="<center><h1>It's a gift! </h1>";

or

$msg_gifted='<center><h1>It\'s a gift! </h1>';

Updated answer

You can not use the same quote inside the string without escaping the quote. So you either use different quotes surrounding the string or you escape it with \. Simplified your code to show different possibilites which will all have the same output.

String in single ' quotes

$msg = '<h1>It\'s a gift!</h1><img src="http://path-to-image.jpg" />';

which is almost the same as tring in double " quotes

$msg = "<h1>It's a gift!</h1><img src=\"http://path-to-image.jpg\" />";

Or string concatenation with different quotes just as you prefer:

$msg = "<h1>It's a gift!</h1>";
$msg.= '<img src="http://path-to-image.jpg" />';
caramba
  • 21,963
  • 19
  • 86
  • 127
  • this corrected the initial issue but now the is not working. The rest of the image url is grey. The $msg_gifted variable is some lines of HTML code actually – xbass540 Oct 12 '17 at 10:12
  • @xbass540 updated the answer – caramba Oct 12 '17 at 10:26
  • all the above were helpfull and managed to control my code better by using the concatenation method. i realized that what was totally braking the code was a
    tag. Is this not allowed in a php variable?
    – xbass540 Oct 12 '17 at 14:02
  • @xbass540 if the answer answered your question please mark as accepted answer. The [
    tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/center) is deprecated and you should not use it. But PHP does not care at all about if you have a `
    ` or any other tags defined as strings. Cause that is all you do. You just put together strings in a variable.
    – caramba Oct 12 '17 at 14:19
0

Put \ like this way:

$msg_gifted='<center><h1>It\'s a gift! </h1>';

This is called escaping ' in string in php.

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33