Voting

: min(three, zero)?
(Example: nine)

The Note You're Voting On

ohcc at 163 dot com
4 years ago
If a constant's name has a leading backslash (\), it's not possible to detect its existence using the defined() function, or to get its value using the constant() function.

You can check its existence and get its value using the get_defined_constants() function, or prepend 2 more backslashes (\\) to the constant's name.

<?php
define
('\DOMAIN', 'wuxiancheng.cn');
$isDefined = defined('\DOMAIN'); // false
$domain = constant('\DOMAIN'); // NULL, in Php 8+ you'll get a Fatal error.
var_dump($isDefined, $domain);
?>

<?php
define
('\DOMAIN', 'wuxiancheng.cn');
$constants = get_defined_constants();
$isDefined = isSet($constants['\DOMAIN']);
$domain = $isDefined ? $constants['\DOMAIN'] : NULL;
var_dump($isDefined, $domain);
?>

<?php
define
('\DOMAIN', 'wuxiancheng.cn');
$isDefined = defined('\\\DOMAIN');
$domain = constant('\\\DOMAIN');
var_dump($isDefined, $domain);
?>

<< Back to user notes page

To Top