March 2, 2012

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

Question by DavidH

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?

Answer by rid

Use defined() and define().

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

Answer by Starx

Use defined() function

if(!defined("constantname")) {
   define("constantname", "value");
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!