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

PHP之define和defined

WBOY
Release: 2016-06-23 14:34:54
Original
1014 people have browsed it

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');
Copy after login
var_dump(defined(newdb));
Copy after login
// output : bool(true)
Copy after login
Copy after login
var_dump(constant('defaultDBName'));// output: string(4) "prod"// 正确用法if(defined('defaultDBName')) {    $db = defaultDBName;    $db = constant('defaultDBName')}// 错误用法if(defined(defaultDBName)) {    // 后果很严重: 下面的这些不会执行的, 不信你试试    $db = defaultDBName;    $db = constant('defaultDBName')}?>
Copy after login



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template