Jump to content

Constants on a Function


fusionpixel

Recommended Posts

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 function

checkConstant();
[/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


while calling the function as
checkConstant(MY_CONSTANT);
the value MY_CONSTAN  got substituded and the value passed is
the value of it eg in FIRST_NAME the value 'Fusion Pixel'
is passed which is not defined

so you need to pass it as
checkConstant('MY_CONSTANT');

inside the function you need to print it as


function checkConstant($theConstant)
{   
if (defined($theConstant))
{
echo constant ($theConstant);
}

}

Try this

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.