|PHP: getting a “use of undefined constant COOKIE_LOGIN” how do I fix this?||#1|
I haven(t made any changes to the code affecting the COOKIE(s and now I get the following:
Use of undefined constant COOKIE_LOGIN - assumed (COOKIE_LOGIN(
if (isset($_COOKIE[COOKIE_LOGIN]) && !empty($_COOKIE[COOKIE_LOGIN]))
setcookie(COOKIE_LOGIN,$objUserSerialized,time() - 86400 );
I(m not sure what I need to do to actually change this since I do not know what chnaged to begin with and so cannot track the problem.
posted date: 2009-04-14 08:03:00
|Re: PHP: getting a “use of undefined constant COOKIE_LOGIN” how do I fix this?||#3|
You need to surround the array key by quotes:
if (isset($_COOKIE[(COOKIE_LOGIN(]) && !empty($_COOKIE[(COOKIE_LOGIN(])) setcookie((COOKIE_LOGIN(,$objUserSerialized,time() - 86400 );
PHP converts unknown literals to strings and throws a warning. Your php.ini probably had the error reporting level to not display warnings but someone may have updated it or something. In either case, it is bad practice to take advantange of PHP(s behavior in this case.For more information, check out the php documentation:Always use quotes around a string literal array index. For example, $foo[(bar(] is correct, while $foo[bar] is not.This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ((bar( - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string (bar( and use that.
posted date: 2009-04-14 08:05:00
|Re: PHP: getting a “use of undefined constant COOKIE_LOGIN” how do I fix this?||#4|
You can(t say
without error unless
is an actual constant that you have defined, e.g.:
Some people have habits where they will write code like:
$ary[example] = 5;echo $ary[example];
Assuming that "example" (as a string) will be the array key. PHP has in the past excused this behavior, if you disable error reporting. It(s wrong, though. You should be using
unless you have explicitly defined
as a constant.
posted date: 2009-04-14 08:08:00
|Re: PHP: getting a “use of undefined constant COOKIE_LOGIN” how do I fix this?||#5|
See also my attempt at a canonical answer for causes of this error message.
posted date: 2011-11-05 22:50:00
|select page: « 1 »|