note 125340 added to function.define

From: Date: Wed, 09 Sep 2020 02:05:12 +0000
Subject: note 125340 added to function.define
Groups: php.notes 
Request: Send a blank email to php-notes+get-215085@lists.php.net to get a copy of this message
If a constant's name has a leading backslash (\), it will be successfully defined using the define() function, but 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 however. Another solution is to 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: 112.195.233.121) ---- Manual Page -- http://php.net/manual/en/function.define.php Edit -- https://master.php.net/note/edit/125340 Del: integrated -- https://master.php.net/note/delete/125340/integrated Del: useless -- https://master.php.net/note/delete/125340/useless Del: bad code -- https://master.php.net/note/delete/125340/bad+code Del: spam -- https://master.php.net/note/delete/125340/spam Del: non-english -- https://master.php.net/note/delete/125340/non-english Del: in docs -- https://master.php.net/note/delete/125340/in+docs Del: other reasons-- https://master.php.net/note/delete/125340 Reject -- https://master.php.net/note/reject/125340 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#215085) next »