Separation of Dev and Production Environments

As a rule developers. programmers and admins tend to be lazy, this is why we have concepts such as shell scripts, crontabs and of course write once, run anywhere. In this case I want to write once but not run anywhere. The condition for not runnning is a determination as to whether or not we want to reach our dev or our production environment. Typically, when I run a dev environment I set up a vhost file that will add an loc prefix to the the domain name for the site I am working on. I will then set my hosts file to direct that vhost's domain name back to an IP on my local machine. For example, when working on my dev version of that domain name would be with a hosts entry pointing to In this exmple I do not want Google Analytics to fire off in a dev environment. By checking the string position of 127 (this should always be the opening segment a local IP address) against, I can quickly determine if I am running in dev or a production environment.

In PHP the logic for checking the host name is as a follows.

strpos(gethostbyname(''), '127')

To avoid running Google Analytics on dev, my code might look like the following.

<?php if(strpos(gethostbyname(''), '127') !== false): ?>
<script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//','ga'); ga('create', 'UA-25758042-1', ''); ga('send', 'pageview'); </script>
<?php endif: ?>