note 125341 added to function.defined

From: Date: Wed, 09 Sep 2020 02:08:56 +0000
Subject: note 125341 added to function.defined
Groups: php.notes 
Request: Send a blank email to php-notes+get-215086@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 45.112.84.4 Probable Submitter: 103.51.167.36 (proxied: 2408:8266:2b01:4a84:19c0:5efd:b1ca:d659) ---- Manual Page -- http://php.net/manual/en/function.defined.php Edit -- https://master.php.net/note/edit/125341 Del: integrated -- https://master.php.net/note/delete/125341/integrated Del: useless -- https://master.php.net/note/delete/125341/useless Del: bad code -- https://master.php.net/note/delete/125341/bad+code Del: spam -- https://master.php.net/note/delete/125341/spam Del: non-english -- https://master.php.net/note/delete/125341/non-english Del: in docs -- https://master.php.net/note/delete/125341/in+docs Del: other reasons-- https://master.php.net/note/delete/125341 Reject -- https://master.php.net/note/reject/125341 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#215086) next »