Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

constants

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?

like image 487
DavidH Avatar asked Mar 02 '12 18:03

DavidH


2 Answers

Use defined() and define().

if (!defined('BASEPATH')) {
    define('BASEPATH', '/mnt/www');
}
like image 138
rid Avatar answered Oct 06 '22 23:10

rid


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

http://us3.php.net/manual/en/function.defined.php

like image 26
Amber Avatar answered Oct 06 '22 22:10

Amber