How to insert Google Adsense Code in PHP script?

Submitted by erick on Sat, 08/19/2017 - 19:45

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>

google_ad_client = "ca-pub-2529029336357825"; google_ad_slot = "7419598997"; google_ad_width = 728; google_ad_height = 90; //--> --

'; //now outputting this to HTML echo $googleadsensecode; ?>

 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.