4

Is there a way to achieve the following?

$myvar = 'x';
debug($myvar); 

// outputs the following 
// myvar value is x

Obviously, for this to happen, the debug function needs to be able to get the variable name passed to it.

Is there a magic constant for that? And if there isn't, please recommend alternative ways that would simplify the debugging.

Of course, I'm aware of the option where you pass the variable name as a separate argument,

 debug('myvar',$myvar);

but my goal is exactly avoid doing that.

benesch
  • 5,239
  • 1
  • 22
  • 36
Average Joe
  • 4,521
  • 9
  • 53
  • 81

4 Answers4

2

Displaying variable name and its value for variable in global scope

Yes, there is, but you will need to pass the name instead:

function debug($var_name) {
    printf('%s value is %s', $var_name, var_export($GLOBALS[$var_name], true));
}

or, if you want only value without the parsable formatting:

function debug($var_name) {
    printf('%s value is %s', $var_name, $GLOBALS[$var_name]);
}

Displaying variable name and its value for variable in local scope

Attention: This works only for variables in global scope. To do the same for local scope, you will probably need a solution employing get_defined_vars(), like that:

printf('%s value is %s', $var_name, get_defined_vars()[$var_name]);

This cannot be simply enclosed within debug() function. This is because get_defined_vars() returns array representing variables in the scope where get_defined_vars() is called, and we do not need the scope where debug() is defined, don't we?

Unified solution

Unified solution could use global scope as default, but also accept some array representing local scope, so the definition could be:

function debug($var_name, $scope_vars=null) {
    if ($scope_vars === null) {
        $scope_vars = $GLOBALS;
    };
    printf('%s value is %s', $var_name, var_export($scope_vars[$var_name], true));
}

and then you can call it like that in global scope:

debug('myvar');

or like that in local scope, passing local scope array:

debug('myvar', get_defined_vars());

Working example

For working example see this demonstration: http://ideone.com/NOtn6

Does it help?

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • tried it but does not work. either the varname or the value is failing. – Average Joe Mar 18 '12 at 23:48
  • @JohnSmith: Please explain what did you exactly mean by "_does not work_". This function works exactly as you mentioned, it only needs to be passed the name instead of the whole variable, as I mentioned. Just look at this example: http://ideone.com/NOtn6 – Tadeck Mar 18 '12 at 23:55
  • Well, the solution has to work for all vars... otherwise, it's mental work trying to think if you are in a function or in the global scope, whether that var was defined globally etc... the localscope example you put together beats the purpose. cause typing get_defined_vars() takes more time than typing 'myvar'. I appreciate your efforts in trying to get this puzzle sorted out. But still no solution. I guess simply this is impossible. – Average Joe Mar 18 '12 at 23:59
  • @JohnSmith: I believe you cannot get further than that. For global variables it works perfectly, except local ones require more work from you. But if you are debugging something, I guess typing "`get_defined_vars()`" (or assigning it to `$v` variable and then using in subsequent `debug()` calls, or even iterating through array of variable names) is shorter than refreshing the window that displays your app. Unfortunately, if you want some more functionality, you will need to change the language ;) PHP does not pass names with variables, as not all are objects (so you cannot store name inside). – Tadeck Mar 19 '12 at 00:08
  • tadeck, I will try your code one more time and see what I can do with it. I'll share my findings back with you, tomorrow or so. I gotto go now. – Average Joe Mar 19 '12 at 00:36
1

There is also great Google Chrome extension PHP Console with php library that allows to:

  • See errors & exception in Chrome JavaScript console & in notification popups.
  • Dump any type variable.
  • Execute PHP code remotely.
  • Protect access by password.
  • Group console logs by request.
  • Jump to error file:line in your text editor.
  • Copy error/debug data to clipboard (for testers).

Recommend everyone!

Community
  • 1
  • 1
barbushin
  • 5,165
  • 5
  • 37
  • 43
0

this is known as print debugging and considered as a very inefficient way for at least four reasons: you waste your time adding and then removing debugging code, debugging code slows down your web, not only the process where you're debugging, you can forget to remove the code after debugging is finished, it's takes time to find and analyze the results -- you see them in the continuous log(s) on the server. A better aproach is to install special debugger extension and work with your code from an IDE inteded for php like PHPEd

0
var_dump($my_var);

dynamic
  • 46,985
  • 55
  • 154
  • 231