php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50331 undefined constant is consider to be string
Submitted: 2009-11-30 10:28 UTC Modified: 2018-03-10 14:50 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: wdolek at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.2.11 OS: GNU/Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: wdolek at gmail dot com
New email:
PHP Version: OS:

 

 [2009-11-30 10:28 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-30 10:38 UTC] derick@php.net
I wouldn't mind kicking this out in PHP 6 either...
 [2010-11-24 10:51 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem
 [2012-03-28 08:40 UTC] 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.
 [2012-03-28 08:43 UTC] 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.
 [2018-03-10 14:50 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-03-10 14:50 UTC] cmb@php.net
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>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 02:01:29 2024 UTC