Why do undefined constants evaluate to true?

本秂侑毒 提交于 2019-12-08 14:35:47

问题


Stupid question - I'm surprised this one has bitten me. Why do undefined constants in PHP evaluate to true?

Test case:

<?php
    if(WHATEVER_THIS_ISNT_DEFINED)
        echo 'Huh?';
?>

The above example prints 'Huh?'

Thanks so much for your help! :)


回答1:


Try defined('WHATEVER_THIS_ISNT_DEFINED')

When PHP encounters a constant that is not defined, it throws an E_NOTICE, and uses the constant name you've tried to use as a string. That's why your snippet prints Huh!, because a non-empty string (which is not "0") will evaluate to true.

From the manual:

If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs "CONSTANT"). An error of level E_NOTICE will be issued when this happens.

If you set your error reporting level to report E_NOTICEs, which is a good practice during development, you will also see the notice thrown.

  • PHP Constant Syntax
  • defined()
  • Casting to Boolean
  • error_reporting
  • error_reporting() function



回答2:


From the manual:

If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs "CONSTANT").

Basically, if WHATEVER_THIS_ISNT_DEFINED isn't defined, PHP interprets it as "WHATEVER_THIS_ISNT_DEFINED". Non-empty strings evaluate to true, so your expression will always pass (unless WHATEVER_THIS_ISNT_DEFINED is defined and set to a falsey value.)

This is, frankly, stupid behaviour. It was implemented, I believe, to allow things like $foo[bar] to work when the programmer should have used $foo['bar']. It's illogical behaviour like this that makes people think PHP isn't a real programming language.

The way to test whether a constant is defined is with defined.




回答3:


Undefined constants are treated as strings by PHP: docs. Taking that fact, think it through in English language:

If "WHATEVER_THIS_ISNT_DEFINED", then do something.

... it is logical that it is "true" - you aren't comparing anything to anything else.

That is why, when doing if statements, it is best practice to include a specific evaluation. If you're checking for false, put it in the code: if (something === false) vs if (something). If you're checking to see if it is set, use isset, and so on.

Also, this highlights the importance of developing with notices and warnings enabled. Your server will throw a notice for this issue:

Notice: Use of undefined constant MY_CONST - assumed 'MY_CONST' in some_script.php on line 5

Turn on notices and warnings to develop, turn them off for production. Can only help!




回答4:


Try defined(). If it's not defined then the constant assumes it's simply text.




回答5:


Note that constant name must always be quoted when defined.

e.g.

  • define('MY_CONST','blah') - correct
  • define(MY_CONST,'blah') - incorrect

also

<?php
 if (DEBUG) {
    // echo some sensitive data.
 }
 ?>
 and saw this warning:
 "Use of undefined constant DEBUG - assumed 'DEBUG'"

A clearer workaround is to use
 <?php
 if (defined('DEBUG')) {
    // echo some sensitive data.
 }
 ?>

See http://php.net/manual/en/language.constants.php




回答6:


It's not just constants, it is a much broader issue with PHP's parsing engine. (You ought to see warnings in your logs.)

In PHP, "bare words" that it doesn't recognize are generally treated as strings that happen to be missing their quotes, and strings with a non-zero length tend to evaluate to true.

Try this:

$x =  thisisatest ;
$y = "thisisatest";
if($x == $y){
    echo("They are the same");
}

You should see "They are the same".




回答7:


Old question, but in addition to defined() you can also use strict type checking using ===

<?php
if(WHATEVER_THIS_ISNT_DEFINED === true) // Or whatever type/value you are trying to check
    echo 'Huh?';


来源:https://stackoverflow.com/questions/6207019/why-do-undefined-constants-evaluate-to-true

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!