Hey so I got tired of writing
echo "varname message";
var_dump(variable);
So I wrote this
function debugger($var, $message) {
echo $message;
var_dump($var);
echo "<br />";
}
Which seems to work fine, except when its in a function. Then its like it doesn’t know that there’s a function defined, because its defined outside of the function. Like so.
function blah() {
$x = 2;
debugger($x, "this is x");
}
Also, I don’t understand functions, I knew you cant reference something in a function outside of the function without returning it, but I didn’t know you couldn’t reference variables or functions outside of the function without setting them as parameters. I think I have this wrong though.
So one more thing, does that mean that the variables inside of a function don’t conflict with the ones outside a function unless its returned?
Your code should work. Something else is wrong in your code. Try being more precise about what is not working and try isolating your problem. As for your other questions:
Variables defined in a function are scoped to that function, and will not interfere with variables from other functions. They also cannot access variables defined outside the function. For example
What you can do is use the
globalkeyword to “include” variables into your function’s scope, if you don’t want to pass them as arguments:Or since a certain version of PHP (not sure which, if anyone knows please edit), you can use
use: