Personal Web Real Estate of Mike Cotton

Primary Navigation

Social Navigation

Making Life Easier with Defines

Programming

PHP CodingWhen adding a new feature to a website or program, it’s always good to have a backup plan for when things go wrong or if the feature doesn’t work out the way it was planned to. At MediaFire, we create what we call defines. In PHP, the way to create a define is:

define("NAME_OF_DEFINE_HERE","value of define here");

For the most part, we use defines to set up configurations that can be changed. This may be an integer that represents a certain amount of seconds or a certain number of bytes. It could also be a boolean value (true or false) that tells you if something is enabled or not.

When adding a new feature, you could always wrap your code inside an if statement using a define. Then, if you want to turn it off, instead of getting rid of all the code you put in, simply change the define. For example, if I created this define:

define("EXAMPLE_ENABLED",true);

Then I wrote this code:

if(EXAMPLE_ENABLED){
echo "<h2>hello world!</h2>";
}

Later on, I can change EXAMPLE_ENABLED to false to turn it off. Now imagine if one feature appeared on several different pages. Use the same if statement everywhere that feature is located, then, if you need to get rid of it, change the define to false and it’s gone in a second!


Mike

Owner of Cotton Web Services and this particular blog. Also one of team members at Holy Fire Games and Modern Tribe. @codingmusician on Twitter