I’m tracing back a variable, I can print out the value of that variable, but I don’t know where(which page) this value has been passed to the variable. Is there any good way to print out where actually this value is coming from?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Recap of debugging and some gotchas.
There is no programmatic way to determine where a variable “spawned” into existence in PHP.
Good debugging skills are your friend. Do check out the other answers posted. Recapped here:
debug_print_backtrace()– to let you know what files are included.grep -rn 'variable_name' .from the top level of your site to see where it exists.var_dump($variable)in a bunch of places one at a time can help as well to track down what the variable was in different states.Also:
Using a good debugger can help out quite a bit. One widely used debugger extension is Xdebug.
Two potential gotchas to look for are:
eval()andextract()Both of these can “magically” cause variables to exist that didn’t before.
Of course, the best way to fix this problem is to not have global variables. Or at least have as few as possible. You can have a well defined flow to your code, whether it’s MVC or something else. Then you’ll know the order your code is executed in and where variables are instantiated and/or passed around.