Is it better to check whether a constant exists before setting it? Or is it ok to just set it directly? I am creating a WordPress plugin and setting a few constants, I’ve seen some plugins that set them directly and others check via if !(defined)
Share
It depends. If you may reasonably expect the constant to already exist, then you should check. If you are sure the constant must not exist at this point because you need to define it, you should not check and let PHP fail hard if your expectation is not met. Errors help to pinpoint problems.