0

I have following code in php where I am binding html code:

 $my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(".$base_image_url.$myDoc->my_doc_name.")' value='".$myDoc->my_doc_id."' class='cropcss'/>";

It produces following output:

<img src="mysite.in/8422_1477013411.png" onclick="window.open(mysite.in/8422_1477013411.png)" value="623" class="cropcss">

In window.open(mysite.in/8422_1477013411.png) I have missed single quote inside. How do I add this?

Blue
  • 22,608
  • 7
  • 62
  • 92
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
  • See: http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php or http://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php - there is a search function... – CD001 Oct 21 '16 at 10:08

3 Answers3

3

I prefer to use single quotes for outputting variables like HTML to the browser. Simply use \' to escape a single quote, and pass it to the browser. Something like this should work:

$my_doc .= '<img src="mysite.in/' . $myDoc->my_doc_name . '" onclick="window.open(\'' . $base_image_url . $myDoc->my_doc_name . '\')" value="' . $myDoc->my_doc_id . '" class="cropcss" />";

If you insist on using double quotes, a minor quick change can fix this (Just add \" around your text):

$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open(\"".$base_image_url.$myDoc->my_doc_name."\")' value='".$myDoc->my_doc_id."' class='cropcss'/>";

For more info, see this post on how to escape quotation marks in php

Blue
  • 22,608
  • 7
  • 62
  • 92
  • I'd argue you *should* use double-quotes or HEREDOC syntax for output as it allows for interpolation of variables - single quotes for things like array keys where it *should* be treated literally. – CD001 Oct 21 '16 at 10:09
  • @CD001 I've always been accustomed to [this style](//stackoverflow.com/a/2373206/4875631) of coding, so it's foreign to me to see it the opposite way. Because it's far easier to escape only the inner single quotes (They appear far less often, usually only in javascript function events), I've always leaned towards using single quotes to output almost anything (And because it's not doing string interpolation, it's a bit faster). – Blue Oct 21 '16 at 10:11
  • @FrankerZ. i have used second method.it works fine.Thanks a lot for saving my lots of hours. – Vision Coderz Oct 21 '16 at 10:15
  • 1
    Personally I'd rather write `echo "Hello {$world} - nice to see you";` than `echo 'Hello ' . $world . ' - nice to see you';` - makes no practical difference of course. I use the same style of HTML quotes as well... but I generally HEREDOC html in the rare cases I'm ever echoing it to screen - normally use a view template with things like `= $var; ?>` embedded to output data from PHP. – CD001 Oct 21 '16 at 10:23
  • ya sure Frankerz – Vision Coderz Oct 21 '16 at 10:25
  • @CD001 For your hello world example, it works great, and does look cleaner, but you're not echoing out html attributes, and mainly using double quotes (That I mentioned was my preferred style), so I don't know if it would look cleaner. – Blue Oct 21 '16 at 10:27
  • @FrankerZ - yeah, sorry, I updated my comment in the interim ;) – CD001 Oct 21 '16 at 10:32
2

As an alternative - you can use HEREDOC syntax - keeps it nice and neat in the output code, no need to worry about escaping quote marks or breaking/concatenating the string.

$my_doc .= <<<MYDOC
<img src="mysite.in/{$myDoc->my_doc_name}" onclick="window.open('{$base_image_url}{$myDoc->my_doc_name}')" value="{$myDoc->my_doc_id}" class="cropcss" />
MYDOC;
CD001
  • 8,332
  • 3
  • 24
  • 28
0

check code replace this

$my_doc.="<img src='mysite.in/$myDoc->my_doc_name' onclick='window.open('".$base_image_url.$myDoc->my_doc_name."')' value='".$myDoc->my_doc_id."' class='cropcss'/>";
pawan sen
  • 716
  • 5
  • 14