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>';
5
  • It seem to always stay the same, so why not just include it statically? Commented Jun 7, 2011 at 18:56
  • 1
    Did you actually test it? 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. Commented Jun 7, 2011 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 \" \". Commented Jun 7, 2011 at 18:57
  • I think I smell a bad editor which does improper syntax highlighting when encountering /*/*/ in strings... Commented Jun 7, 2011 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. Commented Jun 7, 2011 at 22:22

6 Answers 6

3
echo <<< EOD

    your html here

EOD;
Sign up to request clarification or add additional context in comments.

2 Comments

marc you have been memeber for 2 years and posted 0 questions. that's a record
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.
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.

Comments

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

Comments

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>';

Comments

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 "

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.