Req #50331 [Opn->Csd]: undefined constant is consider to be string
| From: | cmb@php.net | Date: | Sat, 10 Mar 2018 14:50:08 +0000 |
| Subject: | Req #50331 [Opn->Csd]: undefined constant is consider to be string | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214270@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=50331&edit=1
ID: 50331
Updated by: cmb@php.net
Reported by: wdolek at gmail dot com
Summary: undefined constant is consider to be string
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: GNU/Linux
PHP Version: 5.2.11
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Indeed, undefined constants raise a notice at least as of PHP
4.3.0, and as of PHP 7.2.0 they raise a warning.[1] As of PHP
8.0.0 they will raise an error.[2]
[1] <https://3v4l.org/55kcP>
[2] <https://wiki.php.net/rfc/deprecate-bareword-strings>.
Previous Comments:
------------------------------------------------------------------------
[2012-03-28 08:43:31] php dot net at doppy dot nl
Forgot to add:
This request is actually already working. You just need to adjust your
error_reporting settings for the error to show up in your error log.
Unless the request is to change the behavior of undefined constants to change to
a fatal error or something similar.
But changing that is probably more problematic for a lot of scripts than the
current behavior.
------------------------------------------------------------------------
[2012-03-28 08:40:02] php dot net at doppy dot nl
When any unknown constant is converted to a string a NOTICE is given.
You have to make sure error_reporting shows these notices.
When I insert this before your code:
<?php error_reporting(-1); ?>
I get the following result:
----------------------------------------
PHP Notice: Use of undefined constant MY_DEFINED_CONSTANT - assumed
'MY_DEFINED_CONSTANT' in ./test.php on line 6
value of constant is TRUE
----------------------------------------
The automatic conversion of a non existing constant to a string is annoying. But
making it a fatal error is probably more problematic.
And off course you get a TRUE result in the expression as the string is not
empty or 0.
I always recommend programming with error_reporting set to *ALL* errors
reported. In production you can reduce that a bit, but when programmed correctly
the logfile will still be small.
------------------------------------------------------------------------
[2009-11-30 10:38:45] derick@php.net
I wouldn't mind kicking this out in PHP 6 either...
------------------------------------------------------------------------
[2009-11-30 10:28:09] wdolek at gmail dot com
Description:
------------
when using undefined constant, PHP consider name of constant to be string. when using this constant
in if () or anywhere else, string value is cast as TRUE, which could confuse developer
Reproduce code:
---------------
// define('MY_DEFINED_CONSTANT', FALSE);
if (MY_DEFINED_CONSTANT) {
echo 'value of constant is TRUE';
} else {
echo 'value of constant is FALSE';
}
Expected result:
----------------
* error or at least warning, that we are using undefined constant *
Actual result:
--------------
value of constant is TRUE
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=50331&edit=1