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&label=ffr456dj5QIQ7YzN2wM&guid=ON&script=0"/>';
$sJS .='</div>';
$sJS .='</noscript>';
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&label=ffr456dj5QIQ7YzN2wM&guid=ON&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
echo <<< EOD
your html here
EOD;
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.
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&label=ffr456dj5QIQ7YzN2wM&guid=ON&script=0"/>
</div>
</noscript>';
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 "
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&label=ffr456dj5QIQ7YzN2wM&guid=ON&script=0"/>
</div>
</noscript>
EOD;
For nowdoc, the only difference would be to put the use EOT with single quotes ('EOT'):
echo <<<'EOT'
...
EOT;