Reputation: 149
I am building an application and I have constant called ENVIRONMENT_SETTING
. As I am currently developing the application I have it as define('ENVIRONMENT_SETTING', 'dev');
Then I am checking at the top of the file what it is. I am currently doing if
statements like so:
if (defined('ENVIRONMENT_SETTING'))
{
if (ENVIRONMENT_SETTING == 'dev')
{
error_reporting(E_ALL);
}
elseif (ENVIRONMENT_SETTING == 'test' || ENVIRONMENT_SETTING == 'prod')
{
error_reporting(0);
}
}
else
{
die('Environment setting not set.');
}
This to me though seems like a bit too much code for such a simple check. Is there a better way to do this?
Upvotes: 0
Views: 41
Reputation: 5090
If you want to keep your die()
part, I consider this form as easier to quickly see in which conditions your script will end / die :
if (defined('ENVIRONMENT_SETTING') || die('Environment setting not set.')) {
if (ENVIRONMENT_SETTING == 'dev')
error_reporting(E_ALL);
elseif (ENVIRONMENT_SETTING == 'test' || ENVIRONMENT_SETTING == 'prod')
error_reporting(0);
}
Upvotes: 0
Reputation: 1185
Well I personally would use a switch statement. It's much more concise. In your case I would do the following:
defined('ENVIRONMENT_SETTING') ? NULL : define('ENVIRONMENT_SETTING', 'dev');
if (defined('ENVIRONMENT_SETTING'))
{
switch (ENVIRONMENT_SETTING)
{
case 'dev':
error_reporting(E_ALL);
break;
case 'test':
case 'prod':
error_reporting(0);
break;
default:
exit('Environment setting not set.');
}
}
Upvotes: 2