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

前端 未结 6 1742
陌清茗
陌清茗 2021-02-19 05:53

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         


        
6条回答
  •  失恋的感觉
    2021-02-19 06:48

    This is condensed rewriting of Radu's answer above (and everyone else's).

    defined('BASE_PATH') || define('BASE_PATH', '/mnt/www/');    
    

    That's it. If defined('BASE_PATH') evaluates to true, the rest of the conditional never gets evaluated. If it returns false, the second half of the conditional is evaluated and BASE_PATH gets defined.

提交回复
热议问题