Home >Backend Development >PHP Tutorial >PHP之define和defined

PHP之define和defined

WBOY
WBOYOriginal
2016-06-23 14:34:541021browse

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')}?>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:my php & mysql FAQNext article:开始做php