Web Hosting Talk







View Full Version : Use of undefined constant notice


latheesan
12-22-2009, 02:31 PM
Hello,

I am working on a script with the following error reporting level:

E_STRICT | E_ALL

This is my script:

function isValidCountryName ($inValue) {

for ($i = 0; $i < __totalCountryList; $i++) {
if (defined(__countryList . $i) && constant(__countryList . $i) == $inValue) {
// Country Name is Valid
return true;
}
}

// Country Name is NOT Valid
return false;
}

if (isValidCountryName('France')) {
// valid
} else {
// not defined
}


This is the notice php generating :

Notice: Use of undefined constant __countryList - assumed '__countryList' in C:\xampp\htdocs\site\activate.php on line 23

Notice: Use of undefined constant __countryList - assumed '__countryList' in C:\xampp\htdocs\site\activate.php on line 23

Notice: Use of undefined constant __countryList - assumed '__countryList' in C:\xampp\htdocs\site\activate.php on line 23

etc...

Any idea why it's doing this?

mattle
12-22-2009, 03:02 PM
Yep...defined() and constant() both require a string argument...you are passing the bareword __countryList (actually, the concatenation operator requires the string conversion first...).

Try this:

defined("__countryList$i") && constant("__countryList$i") //...

latheesan
12-22-2009, 03:12 PM
ah, thanks allot, i didnt know that one :)

petteyg359
12-22-2009, 03:13 PM
By convention, constant identifiers are always uppercase.

PHP Manual: constant() (http://www.php.net/manual/en/constant) clearly states that constant() expects a string. Did you mean "__countryList".$i?

latheesan
12-22-2009, 04:30 PM
yes, this is what i shoud've done:

"__countryList". $i

i assumed using __countryList is a constant and $i is a variable, so i didnt think i had to pass __countryList in as a string and concat with the $i variable

this makes sense now, thanks u2