Page 1 of 1
The best way to see variables?
Posted: Mon Aug 14, 2006 12:20 pm
by jmilane
Hello,
I am hacking some code and I am having trouble figuring out how to determine what the value of variables are as the code is being executed.
isnt working... it seems to just get blown over and passed. Maybe it gets executed... just really fast and I cant see it?
Is there some kind of debugging tool that will let you see what scripts are called and what the value of variables are as it is executed? We used to have something like this with SQLServer and VB/PowerBuilder. I am not aware of any debugging tools like this for php.
Thanks a lot. I do appreciate your time.
Posted: Mon Aug 14, 2006 12:25 pm
by feyd
There have been several threads/posts with "dump" functions that will display the contents of variables. var_dump(), var_export(), and print_r() are a few native functions that are used for simple debugging.
Code: Select all
function dump()
{
foreach(func_get_args() as $arg)
{
$dump = var_dump($arg, true);
if (php_sapi_name() != 'cli')
{
echo '<pre>' . htmlentities($dump, ENT_QUOTES, 'UTF-8') . '</pre>';
}
else
{
echo $dump;
}
}
}
is one possible (untested) dump function.
Posted: Mon Aug 14, 2006 12:26 pm
by nincha
try:
Code: Select all
echo 'the variable value is ->' . $var;
you can also exit(1); after you echo it.
Posted: Mon Aug 14, 2006 12:31 pm
by jmilane
nincha wrote:try:
Code: Select all
echo 'the variable value is ->' . $var;
you can also exit(1); after you echo it.
This gets me out, but I see no variables.
Code: Select all
function dump()
{
foreach(func_get_args() as $arg)
{
$dump = var_dump($arg, true);
if (php_sapi_name() != 'cli')
{
echo '<pre>' . htmlentities($dump, ENT_QUOTES, 'UTF-8') . '</pre>';
}
else
{
echo $dump;
}
}
}
Does not appear to work.
and
does not either - I will look into it... I might be (probably am) using it wrong.
Posted: Mon Aug 14, 2006 12:38 pm
by feyd
you need to call dump() with something for it to show.. such as dump('foo') or dump($a,$b,$c).
Posted: Mon Aug 14, 2006 12:46 pm
by jmilane
feyd wrote:you need to call dump() with something for it to show.. such as dump('foo') or dump($a,$b,$c).
Okay, but what if I wanted to see ALL the variables that are being passed to a script?
Here's the thing... I have a form that posts to a .php file.
That file is way complex. It calls other files.
I dont know which file does what, so I want to be able to see the value of the different variables being passed around.
It I could actually see what functions are being called, what variables are being passed, and where they are, it would solve everything... but I cant.
I cant even locate where the actual SQL insert is executed.
Posted: Mon Aug 14, 2006 12:49 pm
by feyd
so try giving it $GLOBALS or $_POST, $_GET, $_SERVER, $_ENV, etc etc.
Posted: Mon Aug 14, 2006 12:50 pm
by nincha
Okay, but what if I wanted to see ALL the variables that are being passed to a script?
try:
Code: Select all
foreach($_GET as $each){
echo $each.'<br/>';
}
foreach($_POST as $each){
echo $each.'<br/>';
}
?
Posted: Mon Aug 14, 2006 12:57 pm
by jmilane
feyd wrote:so try giving it $GLOBALS or $_POST, $_GET, $_SERVER, $_ENV, etc etc.
This is helpful. You rock. Thanks... and sorry I dont know this stuff... but I am learning.
Posted: Mon Aug 14, 2006 12:58 pm
by jmilane
nincha wrote:Okay, but what if I wanted to see ALL the variables that are being passed to a script?
try:
Code: Select all
foreach($_GET as $each){
echo $each.'<br/>';
}
foreach($_POST as $each){
echo $each.'<br/>';
}
?
This is cool and would actually be perfect - but it doesnt give me the name of the variable...
*EDIT* - Almost got it figured out. I dont think my variables are called what I thought they were called. Or, put more lucidly, I think their names change from $var to part of an array. I dont know, but Im getting closer. Thanks a lot.