The best way to see variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

The best way to see variables?

Post 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.

Code: Select all

echo $var;
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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

try:

Code: Select all

echo 'the variable value is ->' . $var;
you can also exit(1); after you echo it.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post 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

Code: Select all

echo vardump();
does not either - I will look into it... I might be (probably am) using it wrong.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you need to call dump() with something for it to show.. such as dump('foo') or dump($a,$b,$c).
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

so try giving it $GLOBALS or $_POST, $_GET, $_SERVER, $_ENV, etc etc.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post 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/>';
}
?
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post 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.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post 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.
Post Reply