Jimmyb
Jimmyb

Reputation: 890

Echoing javascript from PHP

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

Upvotes: 2

Views: 690

Answers (6)

Zach Lysobey
Zach Lysobey

Reputation: 15744

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;

Upvotes: 1

Fosco
Fosco

Reputation: 38526

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

Upvotes: 1

Cfreak
Cfreak

Reputation: 19319

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 "

Upvotes: 1

Wrikken
Wrikken

Reputation: 70540

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

Upvotes: 1

Stephane Gosselin
Stephane Gosselin

Reputation: 9148

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.

Upvotes: 2

dynamic
dynamic

Reputation: 48141

echo <<< EOD

    your html here

EOD;

Upvotes: 3

Related Questions