Heim >Backend-Entwicklung >PHP-Tutorial >PHP之define和defined
php的define用来定义常量, defined用来判断常量是否定义, 不过里面有个小小的陷阱... 看下面的例子吧:
<?phpdefine ('defaultDBName','prod');var_dump(defaultDBName);// output: string(4) "prod"var_dump(defined(defaultDBName));// output: bool(false)var_dump('defaultDBName');// output: string(13) "defaultDBName"var_dump(defined('defaultDBName'));// output: bool(true)define('newdb', 'defaultDBName');
var_dump(defined(newdb));
// output : bool(true)
var_dump(constant('defaultDBName'));// output: string(4) "prod"// 正确用法if(defined('defaultDBName')) { $db = defaultDBName; $db = constant('defaultDBName')}// 错误用法if(defined(defaultDBName)) { // 后果很严重: 下面的这些不会执行的, 不信你试试 $db = defaultDBName; $db = constant('defaultDBName')}?>