Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error when checking for constant using constant() function 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I have
Code:
if(constant(APPTITLE) == NULL) {
	define ('APPTITLE' ,'POINT OF SALE');		// Application Title
	define ('APPCOMPANY','MY BAR NAME');			// Name of company or banner title
	define ('APPURL' ,'[URL unfurl="true"]http://localhost/pos/');[/URL]			// Application URL

I want to make sure that if the config.php script is called a 2nd or 3rd ... time, then constant variables are not "re-defined" thus avoiding the error this would cause.

Instead, I am getting
Code:
Warning: constant() [function.constant]: Couldn't find constant APPTITLE in C:\wamp\[URL unfurl="true"]www\pos\bin\config.php[/URL] on line 11

What am I missing?

Thank you all in advance for you assistance!



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
the constant() function returns the value of the constant. If no constant is set then an error (at level E_WARNING) is thrown and the NULL value returned. this is correct behaviour.

to find out whether a constant has been defined use the defined() function.

if you wanted to know whether a constant was defined and is null then use this

Code:
if(is_defined('CONSTANT') && is_null(CONSTANT)):

endif;
 
jpadie, thanks for the explanation. I ended up using if(!defined('CONSTANT') instead. I had misread the definition of the function and missed the use of the quotes to enclose the constant name.

Regards,



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
my codesnip above is wrong. it should refer (as in the body of my post and your response) to
Code:
defined("CONSTANT")
and not is_defined().

I prefer the is_* prefix (like isset() and is_string() etc) so I have the following function in a library that is always included in my applications. hence the mistake. sorry.

Code:
function isDefined($constant){
  return defined($constant);
}
function is_defined($constant){
 return defined($constant);
}

is have similar contraction functions for other is_* functions. it's lazy of me but a minimal performance hit and at my age it's easier to do this than change the way I think ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top