Forum Moderators: coopster

Message Too Old, No Replies

[Whats make this error] PHP Notice: Use of undefined constant

         

basketmen

10:15 pm on Apr 10, 2015 (gmt 0)

10+ Year Member



I just moved to new host
In this new host i always got error_log, even with this simple php file :
<?php define(Y,120); ?>


this is the error_log content:

[09-Apr-2015 18:42:09 UTC] PHP Notice: Use of undefined constant Y - assumed 'Y' in /home/username/public_html/test.php on line 1



i can add single quote wrapping the Y like this to fix it
<?php define('Y',120); ?>


but there are a lot of code like that, it is better if i dont need to change the files,
but just change the hosting required, what is it actually? is it because using litespeed?

in old hosting there is cgi-fcgi information
[freakimage.com...]

in new hosting is using litespeed
[freakimage.com...]

whitespace

1:12 am on Apr 11, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



i can add single quote wrapping the Y like this to fix it


So, you appreciate why you are getting this "Notice"?

This is a PHP notice (a mild warning from PHP telling you that it looks like there could be a problem, but it might work anyway - because PHP "helpfully" defaults things). It doesn't relate to your hosting, other than the fact that PHP could be set up differently.

You are getting this notice reported at your new host because the error_reporting level is set higher. You would have had the same "problem" at your old host, except it just wasn't being reported.

The correct approach is to fix this problem. However, if you are absolutely sure your code works OK and has been working OK then you can simply lower the error_reporting level to exclude E_NOTICE messages. At the very top of your script:

error_reporting(E_ALL & ~E_NOTICE);


Depending on your version of PHP you might still need some adjustments.

basketmen

12:08 pm on Apr 11, 2015 (gmt 0)

10+ Year Member



thank you, gbu man

whitespace

12:48 pm on Apr 11, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



You're welcome.

Although whilst you are developing the site you should really enable the reporting of E_NOTICE (and E_STRICT) messages as these can catch potentially crippling runtime errors. Some E_NOTICE messages can be the result of obvious typos in the code or highlight potential security vulnerabilities.

error_reporting(E_ALL | E_STRICT);


(In PHP 5.4+ you only need E_ALL since this includes E_STRICT by default.)

Readie

3:13 pm on Apr 15, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the record, you should quote the Y in define(). The first parameter of the define function expects a string. An undefined constant gets defaulted to the string version of the constant name you are trying to use.

So, basically what you're doing above, is using the constant Y when trying to define the constant Y. PHP being PHP defaults it to 'Y' and then allows you to define Y. Which I find brilliant :D

whitespace

8:51 pm on Apr 15, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



PHP being PHP defaults it to 'Y' and then allows you to define Y. Which I find brilliant :D


The chicken / egg problem - solved! Albeit with a little E_NOTICE!

However, if you then (accidentally) try to define the same constant again (ie. without the quotes) then you end up with another constant being defined whose name is the value of the constant you have just defined, ie. (int)120! Whose value is also (int)120! Thus polluting the symbol table, and no notice/warning this time!

If you'd correctly used a string to define these constants (as Readie mentions) then the second call would have resulted in an E_NOTICE ("Constant Y already defined") and nothing untoward happens.