2

I am trying to echo some google analytics javascript code from PHP so it can be conditionally read based on specific scenarios. I'm having difficulty wrapping my head around the quoting since the code contains /* */ characters. I'm looking for some direction in assigning this type of text to a php variable.

Thanks

$sJS .='<script type="text/javascript">';
$sJS .='/* <![CDATA[ */"';
$sJS .='var google_conversion_language = "en";';
$sJS .='var google_conversion_format = "2";';
$sJS .='var google_conversion_color = "ffffff";';
$sJS .='var google_conversion_value = 0;';
$sJS .='/* ]]> */';
$sJS .='</script>';
$sJS .='<script type="text/javascript" src="http://www.googleadservices.com/page.js">';
$sJS .='</script>';
$sJS .='<noscript>';
$sJS .='<div style="display:inline;">';
$sJS .='<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/997410413/?value=1.00&amp;label=ffr456dj5QIQ7YzN2wM&amp;guid=ON&amp;script=0"/>';
$sJS .='</div>';
$sJS .='</noscript>';
Jimmyb
  • 860
  • 1
  • 8
  • 22
  • It seem to always stay the same, so why not just include it statically? – McKayla Jun 07 '11 at 18:56
  • 1
    Did you actually test it? http://ideone.com/6GfMD . IF it looks like it's a comment in there, I suggest you should change your IDE because its syntax highlighting sucks. – Gabi Purcaru Jun 07 '11 at 18:56
  • 1
    What is the question? `/* */` is perfectly legal as part of a string. It only acts as a comment in normal code, not when inside a string. Note the use of `' '` rather than `" "` as the double quotes are used in the Javascript, otherwise you'd have to escape them like `\" \"`. – MGwynne Jun 07 '11 at 18:57
  • I think I smell a bad editor which does improper syntax highlighting when encountering `/*`/`*/` in strings... – Wrikken Jun 07 '11 at 18:59
  • Yup, the syntax highlighting threw me off course. Thanks for the quick how-to! However, I wasn't aware of the heredoc capability, very nice bonus find today. – Jimmyb Jun 07 '11 at 22:22

6 Answers6

3
echo <<< EOD

    your html here

EOD;
dynamic
  • 46,985
  • 55
  • 154
  • 231
  • marc you have been memeber for 2 years and posted 0 questions. that's a record – dynamic Jun 07 '11 at 22:30
  • I'd love to post some, but the system I'm working on right now is such a niche product, and dealing with such a particular subset of the system, I doubt there'd be many takers. – Marc B Jun 08 '11 at 03:23
2

It like to use heredoc syntax for that sort of stuff.

Just have your js in a seperate include with the heredoc variable, it makes a much cleaner style.

Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
1

What's wrong with:

<?php
$JS='
<script type="text/javascript">
/* <![CDATA[ */"
var google_conversion_language = "en";
var google_conversion_format = "2";
var google_conversion_color = "ffffff";
var google_conversion_value = 0;
/* ]]> */
</script>
<script type="text/javascript" src="http://www.googleadservices.com/page.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/997410413/?value=1.00&amp;label=ffr456dj5QIQ7YzN2wM&amp;guid=ON&amp;script=0"/>
</div>
</noscript>';
Wrikken
  • 69,272
  • 8
  • 97
  • 136
1

Well you don't say exactly what's not working. A quoted /* shouldn't be treated as a comment. It's likely you have mismatched quotes elsewhere in your code if this isn't giving you what you expect. Just at a glance i see:

$sJS .='/* <![CDATA[ */"';

It looks like you have " and then ' at the end. You probably don't need the "

Cfreak
  • 19,191
  • 6
  • 49
  • 60
1

You don't need to do it line by line like that... PHP supports continuation to the next line. This works just fine:

$sJS = '<script type="text/javascript">
    /* <![CDATA[ */
    var google_conversion_language = "en";
    var google_conversion_format = "2";
    var google_conversion_color = "ffffff";
    var google_conversion_value = 0;
    /* ]]> */
    </script>
    <script type="text/javascript" src="http://www.googleadservices.com/page.js">
    </script>
    <noscript>
    <div style="display:inline;">
    <img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/997410413/?value=1.00&amp;label=ffr456dj5QIQ7YzN2wM&amp;guid=ON&amp;script=0"/>
    </div>
    </noscript>';

Your string is quoted with single quotes, there are no single quotes inside of it, and there are no backslashes or escape sequences.

Visibly working (if you view source) at: http://gfosco.kodingen.com/phpjs.php

Fosco
  • 38,138
  • 7
  • 87
  • 101
1

You can use either the Heredoc or Nowdoc syntax to accomplish this easier.

Nowdoc (in php > 5.3) will not parse variables (similarly to single quotes)

Heredoc solution:

echo <<< EOD

<script type="text/javascript">
/* <![CDATA[ */"
var google_conversion_language = "en";
var google_conversion_format = "2";
var google_conversion_color = "ffffff";
var google_conversion_value = 0;
/* ]]> */
</script>
<script type="text/javascript" src="http://www.googleadservices.com/page.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/997410413/?value=1.00&amp;label=ffr456dj5QIQ7YzN2wM&amp;guid=ON&amp;script=0"/>
</div>
</noscript>

EOD;

For nowdoc, the only difference would be to put the use EOT with single quotes ('EOT'):

echo <<<'EOT'
...
EOT;
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149