Just a quick question regarding how PHP handles switch statements.
If I had the following code
switch (APPLICATION_ENVIRONMENT) {
case 'production':
echo 'production';
break;
case 'stage':
echo 'stage';
break;
default: //dev
echo 'dev';
}
would this still evaluate to default if APPLICATION_ENVIRONMENT was not defined anywhere? Or would it throw out an error? Looking at existing source in the application im running, whoever has done this previously has done an if(defined()) on the constant first to check if it exists, which is a waste if switch can eval that properly for me
Thanks DJ
It would evaluate the switch, however it would throw an error.
Try this instead:
$env = defined('APPLICATION_ENVIRONMENT') ? APPLICATION_ENVIRONMENT : null;
switch($env) {
..
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With