26

I am trying to create a php variable that has three levels of nested quotes. How do I make a third level around "tackEvent", "downloads", "all", and "nofilter"? The double quotes that I have there are not working.

  $outputList .= "<a href=files/".$content_file ." onClick='_gaq.push
(["_trackEvent", "downloads", "all", "nofilter"]);' >" . $content_name . 
"</a>";
JSW189
  • 6,267
  • 11
  • 44
  • 72

4 Answers4

39

From here:

  • Outer quote = " (This marks the beginning and end of the string)
  • Inner quote = \" (Escaped as to not flag "beginning/end of string")
  • Third-tier quote = ' (Literal quote)
  • Fourth-tier quote = \' (Literal quote that will be generated as an escaped outer quote)
Nick Brunt
  • 9,533
  • 10
  • 54
  • 83
  • Sometimes is more convenient to use `'` at second level, so only need to start escaping at third level. That is, switch "inner" and "third-tier". – ToolmakerSteve Jul 15 '20 at 19:38
3
  • Outer quote: "
  • Inner quote: '
  • Third-tier quote: \"
  • Fourth-tier quote: &quot;
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Fourth quote is & q u o t ; – Joaquim Neto Jun 07 '17 at 20:27
  • 1
    This does not answer the question. _why_ do you believe this is the answer? _how_ does it work? Simply telling someone to change their code without any context or meaning does not help them learn what they did wrong. – GrumpyCrouton Jun 07 '17 at 20:33
2
$outputList .= <<<LINK
<a href="files/$content_file" onClick="_gaq.push(['_trackEvent', 'downloads', 'all', 'nofilter']);">$content_name</a>
LINK;

This is using heredoc syntax.

FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
1

From the manual:

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\).

This applies to strings in double quotes as well.

$str = "I am a string with a quote that says, \"I like quotes\"";
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96