1

I created a function to allow me to debug PHP scripts so long as a variable ($debug) is set to 1:

function debug($msg) {
    if ($debug == 1) {
        echo $msg;
    } else {
        return false;
    }
}

That way, at the top of my script (before the functions.php file is called), I write:

$debug = 1;

to enable debugging, then:

debug("Function executed: " . $data);

at certain points so that I know string values/whatever at that point, with the desired response being the message displayed upon the screen.

However, regardless of what the value of the $debug string is, I never see any echo'd statements.

How can this be achieved?

6
  • 1
    How about using a debugger? Commented May 29, 2013 at 12:19
  • @zerkms I think he means running the code in development mode instead of production. So he needs to show certain messages. He's not referring to actual code debugging :) Commented May 29, 2013 at 12:20
  • Yes exactly, thank you itsols. Sorry it wasn't so clear! Commented May 29, 2013 at 12:22
  • Where did you read that functions can access any variable that's specified above it? Commented May 29, 2013 at 12:28
  • The same book where you read that sarcasm helps solve issues. I'm not experienced in PHP, I'm just after a bit of help! Commented May 29, 2013 at 12:29

5 Answers 5

5

Debug is not available to your function because it is out of scope. You either:

  1. Need to pass it as a parameter
  2. Use the global keyword to include it in your function (discouraged)

.

function debug($msg, $debug){
    if($debug==1){
    echo $msg;
} else {
    return false;
    }
}

or

function debug($msg){
    global debug;
    if($debug==1){
    echo $msg;
} else {
    return false;
    }

}

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for this. The point of the function was so that I didn't have to comment out all of the individual echo commands, so passing it as a parameter is creating the same issue. Why should creating a global keyword be discouraged?
The word global is not discouraged itself, using global variables is. The problem might be if you use this global variable in function that is shared by many projects or there is possibility that in some conditions the global variable is not set. Your function should be logically dependent only on what you pass as their arguments, nothing more. Somebody who may use this in future must know that its args are the only things he must provide, that there is nothing more to do.
Thank you for your help. I have been successful in recreating the function using constants instead. Your answer was great, thanks.
@JohnConde I think in the second case it would be also good to check isset($debug)
2

It's difficult to say because you provided too few data.

The reason can be that your $debug variable is not known inside a function. Because using globals is not adviced, consider using constants define("DEBUG",1);.

EDIT

I presented within another question how I use a class for doing the same thing as class names are also globally accessed.

2 Comments

Thank you for your help! I have been successful in recreating the function using constants instead.
@BenPearlKahan In fact you could also check whether a constant exists; use defined() function
0

The global variable is not accessible to functions until you make it so. Eg:

function debug($msg( {
   global $debug;
   etc...

PS: Please, don't do this. Find a better way. Really.

2 Comments

The point of the function was so that I didn't have to comment out all of the individual echo commands, so passing it as a parameter is creating the same issue. Could you suggest a better alternative?
Logging. But a define() would be better than a global.
0

$debug is your global variable, so it is not accessible in your function

Comments

0

There is also the possibility to declare a const, (and then just insure the namespace is correct), like this:

const debug = true;
function newPrint($msg) {
  if (\debug === true) {
      echo $msg;
  } else {
      return false;
  }
}
\newPrint("heey");//will print "heey"

so just dont use a variable, but use a const.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.