I want to do an old C/C++ trick where I can conditionally define a function. for instance I want to conditionally output a string (debugging) so I can have some outputstring() interspersed in my code. Instead of adding/removing these during development, I want to turn them on/off.

Is there a way to conditionally define the following function so that if its not wanted the function gets turned into a NULL argument that never gets called?

function outputstring($string)
{
 echo "$string";
}

Thanks,

Pete

You can put an IF ahead of the Function statement. You just need a parm somewhere to drive the IF. I don't think however; that will give you what you want. If you leave the calls to the Function in your code and it isn't defined, you'll get an error.

I have done something similar to what you want to do with my own debug routines. I leave the debug statements in the code and turn debugging off or on at the start of the module. At the start of each debug function, it checks the global parm to see if debugging is turned on. If not, it exits immediately. I think you need to do the same thing.

Member Avatar for rajarajan2017
if (need) outputstring($string)

We can call with the condition but I hope not able to define like that.

If we put a piece of code in like this:

if(isset($debug)) OutputString("got to here!");

The runtime interpreter still has to evaluate each and every call. Obviously, if we're debugging that's unnecessary but OK. However, if we're NOT debugging then it becomes unnecessary overhead which effects performance.

The same holds true if we do this:

function outputstring($string)
{
  if(isset($debug)) echo "$string";
}

I was trying to find something like this:

if(isset($debug))
{
  DEFINE('OUTPUT_STRING', 'OutputString("$string");');
}
else
{
  DEFINE('OUTPUT_STRING', '');
}

This has one problems: $string still has to be set which creates overhead and puts us back to square one. (Plus, I dont know that I can actually do that without producing a parse error... but I dont see why not.

The best I have come up with is maybe this but its very painful:

if(isset($debug))
{
  DEFINE('OUTPUT_STRING_THIS_ERROR', 'OutputString("func_name(): Got to here!");');
  DEFINE('OUTPUT_STRING_THAT_ERROR', 'OutputString("func_name(): Variable = $variable ");');
}
else
{
  DEFINE('OUTPUT_STRING_THIS_ERROR', '');
  DEFINE('OUTPUT_STRING_THAT_ERROR', ''};
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.