Search code examples
phpconstants

Does PHP have an equivalent of C/C++'s #ifdef?


I'm trying to define a constant, but I don't want to redefine it if it's already been defined. Here's a C sample:

#ifndef BASEPATH
#define BASEPATH /mnt/www
#endif

What is the most elegant way to do this in PHP?


Solution

  • Use defined() and define().

    if (!defined('BASEPATH')) {
        define('BASEPATH', '/mnt/www');
    }