If its only "avoid debugs in production" then use the built in assert() function.
This avoids your debug messages from being processed at all and prevents any calculations you might have in there from consuming memory or processing power.
assert is completely ignored in production.
function debug(...$stuff){
var_dump($stuff);
return true; //always return true so assert will always pass it.
}
assert(debug("This code will not even be processed on production server. "));
That's the safest way but not very flexible.
One method I have been using a lot is to check the host name or the client IP address.
So I have a function to check the host name like this:
function onTestServer(){
$SERVER_NAME = $_SERVER["SERVER_NAME"];
if(substr($SERVER_NAME,-5)==="te.st") return true;//all *.te.st domains are test servers
if (strpos($SERVER_NAME, ".localhost")!==false) return true; //all *.localhost domains are test servers
return false;
}
And then I have my own vardump, debug and die statements that wont execute in production similar to this:
function varDump($stuff){
if (!onTestServer()) return;
echo "<pre>";
var_dump($stuff);
echo "</pre>";
}
Then you just only ever use the custom varDump() function so only you can see it.
My debug functions are a lot more complicated and show expandable trees of information etc. But the basic principle is the same. if on localhost do the vardumps etc otherwise nothing is displayed.
Really handy if you always want to see extra info when testing but dont want to have to remove all the vardumps before deploying to the server.
I also do things like check the client IP address and display errors and info on the actual production server as well if the client ip address is my own.
function getIp()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
function onTestClient(){
$office_ip = "xxxx.xxxx.xxxx.xxxx";
$test_client_ips = array($office_ip,"any other ip you want");
$client_ip = getIp();
if (in_array($client_ip,$test_client_ips)) return true;
return false;
}
Can be extremely handy if you need to test some quick updates on a live site from a handy computer without local testing environment to use. Can just add your current IP to the list and use functions like this one:
function die_dev($message){
if (onTestServer() || onTestClient()) die($message);
}
Or even:
if (onTestClient()){
//show a completely different interface/webpage html js etc
} else {
//show the original content that is currently live
}