How to insert Google Adsense Code in PHP script?

Submitted by erick on Tue, 08/02/2016 - 00:32

I have tried this and it is confusing at first, anyway the best practice to insert the Google adsense code is to assign this to a PHP variable:

Example of adsense code:

<script type="text/javascript">
google_ad_client = "pub-123456789";
google_ad_slot = "123456";
google_ad_width = 180;
google_ad_height = 150;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

 So to assign this code to a PHP variable and then outputting to an HTML:

<?php
//assign adsense code to a variable
$googleadsensecode = '
<script type="text/javascript">
google_ad_client = "pub-123456789";
google_ad_slot = "123456";
google_ad_width = 180;
google_ad_height = 150;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>';
//now outputting this to HTML
echo $googleadsensecode;
?>
 

What is good with this approach is that you will not be needing any PHP include or require functions which might be disabled in some free hosting accounts.