开发者

PHP CONSTANT not set but still prints something to screen?

Isn't PHP suppose to show an error is you call a non-existent CONSTANT? When I run the code below for a constant that is not defined, it shows on the screen "TEST" instead of any kind of error. Could I have a setting wrong in my php.ini file or is开发者_运维问答 this something new? Im running PHP 5.3

<?php
echo TEST;
?>


It's supposed to show a notice which you may not have set as visible (E_NOTICE). On default, if the constant is not defined, it shows the literal value.


There's a default in php for an undefined constant to be silently replaced by a string representing its name. I think it can be disabled in php.ini, but it shouldn't be relied upon in any case.


This is just a warning or a notice, I'm not quite sure. The use of an undefined constant is not prohibited. However, echo FOO BAR; would indeed fail, because it's equivalent to echo "FOO" "BAR";.

Either your error_reporting setting does not show warnings or notices, or your display_errors setting is set to false.

You should not rely on this.


As the others have stated, referencing an undefined constant is a notice, not a fatal error (one that would cause PHP to stop running the script), and PHP will convert the constant name to a string and move on. Depending on your error_reporting setting, PHP might or might not print a message. You need to set it to at least 8 (E_NOTICE) before PHP prints the notice message. If what you want is to catch the undefined constant situation and handle it (e.g. have PHP print a message and exit), use the defined function like this:

if (defined('TEST')) {
    echo TEST;
} else {
    // error handling here
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜