fusionpixel Posted December 8, 2006 Share Posted December 8, 2006 I am trying to create a function that will check if a Constant has been defined.one way to do it is:[code]#define Constant:define('FIRST_NAME', 'Fusion Pixel');[/code][code]if ( defined('FIRST_NAME') ){ echo FIRST_NAME;}[/code]But I need a function to call it through the code:[code]function checkConstant(){ if ( defined('FIRST_NAME') ) { echo FIRST_NAME; }}#now call the functioncheckConstant();[/code]But the problem is that if I have different constants that I need to check it would be nice to have a function that I can pass the function to without having to rely on one function at the time. Something like[code]checkConstant(MY_CONSTANT);[/code]But it doesn't work:[code]function checkConstant($theConstant){ if ( defined($theConstant) ) { echo FIRST_NAME; }}[/code]The function above failes silently.Any ideas? Link to comment https://forums.phpfreaks.com/topic/29885-constants-on-a-function/ Share on other sites More sharing options...
logu Posted December 8, 2006 Share Posted December 8, 2006 while calling the function ascheckConstant(MY_CONSTANT);the value MY_CONSTAN got substituded and the value passed isthe value of it eg in FIRST_NAME the value 'Fusion Pixel'is passed which is not definedso you need to pass it as checkConstant('MY_CONSTANT');inside the function you need to print it asfunction checkConstant($theConstant){ if (defined($theConstant)) { echo constant ($theConstant); } }Try this Link to comment https://forums.phpfreaks.com/topic/29885-constants-on-a-function/#findComment-137321 Share on other sites More sharing options...
fusionpixel Posted December 8, 2006 Author Share Posted December 8, 2006 Nice, I can't believe it. so close yet so far.I tried to read books and the PHP manual to figure this out but still for some reason I couldn't figure it out.Thanks for the tip, it works like a charm. Link to comment https://forums.phpfreaks.com/topic/29885-constants-on-a-function/#findComment-137327 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.