Assign Google Adsense Code to a PHP Variable

Assigning Google Adsense code to a PHP variable doesn’t work the way you would expect it to. I thought just deleting the newline characters and extra spaces from the adsense code and then assigning the resulting text to a variable should work. However it does not, and I’m not sure why. However what does work is shown below.

<?php
$googleadsensecode = '
<script type="text/javascript">
google_ad_client = "pub-123456789";
google_ad_slot = "123456";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';
echo $googleadsensecode;
?>

The following example does not work.

<?php
$googleadsensecode = '<script type="text/javascript"> <!-- google_ad_client = "ca-pub-123456789"; google_ad_slot = "123456"; google_ad_width = 300; google_ad_height = 250; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>';
echo $googleadsensecode
?>

About Pratik Sinha

Linux Nerd, Socialist, Atheist, Adventuristic, Nature Lover, Geeky.

1 comment

Leave a Reply to templaries Cancel reply