Debug simplification

Is there any way to achieve the following?

$myvar = 'x'; debug($myvar); // outputs the following // myvar value is x 

Obviously, for this, the debugging function must be able to get the variable name passed to it.

Is there a magic constant for this? And if this does not happen, try alternative ways to simplify debugging.

Of course, I know an option in which you pass the variable name as a separate argument,

  debug('myvar',$myvar); 

but my goal is to avoid it.

+4
source share
4 answers

Displays the name of a variable and its value for a variable in the global area

Yes, there is, but you will need to specify a name instead:

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

or, if you only want a value without formatting with parsing:

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

Display the variable name and its value for the variable in the local area

Note: this only works for variables in the global scope. To do the same for the local area, you probably need a solution using get_defined_vars() , for example:

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

It cannot be simply wrapped in a debug() function. This is because get_defined_vars() returns an array representing the variables in the area where get_defined_vars() is get_defined_vars() , and we don’t need the area where debug() defined, right?

Single solution

A unified solution can use the global scope by default, but also accept some array representing the 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 globally:

 debug('myvar'); 

or similarly in a local area, passing in a local array of areas:

 debug('myvar', get_defined_vars()); 

Working example

For a working example, see this demo: http://ideone.com/NOtn6

Does it help?

+2
source

There is also a great Google Chrome extension PHP Console with php library , which allows you to:

  • See errors and exceptions in the Chrome JavaScript console and notification pop-ups.
  • Dump any type variable.
  • Remote execution of PHP code.
  • Password Access Protection.
  • Group console logs on request.
  • Go to error file: line in a text editor.
  • Copy errors / debugging data to the clipboard (for testers).

Recommend to everyone!

+1
source
 var_dump($my_var); 

0
source

this is called print debugging and is considered a very inefficient way for at least four reasons: you spend your time adding and removing debugging code, the debugging code slows down your website and not just the debugging process, you may forget to remove the code after debugging is complete It takes time to find and analyze the results - you see them in a continuous log (s) on the server. The best aproach is to install a special debugger extension and work with your code from the IDE designed for php like PHPEd

0
source

Source: https://habr.com/ru/post/1402149/


All Articles