What php variable debugging function are you using? var_dump, print_r, var_export, other?

I personally use var_dump , but many people like print_r .

What is everyone using? Advantages and disadvantages?

Does anyone have a special home brew function?

+4
source share
9 answers

I always use the extended var_dump Xdebug. It produces a lot of detailed output.

See http://xdebug.org/docs/display for more details.

+4
source

I use print_r() because I like the massive structure of the array ... but var_dump gives you a little more information (e.g. types)

 $obj = (object) array(1, 2, 3); // output of var_dump: object(stdClass)#1 (3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } // output of print_r stdClass Object ( [0] => 1 [1] => 2 [2] => 3 ) 
+4
source

I use these custom functions depending on whether I am dealing with an array or with a single value:

 function show($array) { echo '<pre>'; print_r($array); echo '</pre>'; } function prn($var) { echo '<br/>' . $var . '<br/>'; } 

I find that these features simplify troubleshooting because I usually have to format the output so that I can easily skip it right on the screen.

For more complex troubleshooting, we use an extended version of the Exception class, which will send a stack trace along with a special error message. This gives me the functions that were involved, which files were involved and which lines or lines were involved in the error, as well as any custom message that I created so that I knew exactly what was going on. For an additional level of troubleshooting, we also log these errors in an accessible database.

+4
source

I am just user print_r, as well as several wrapper functions for storing the various DebugPrint that I inserted into my code, and one on the footer to unload the stack on the page (or in the file).

Now I'm also trying to use XDebug ...: - D

OK, for the record I give my little functions ...

 // Primitive debug message storage // $level = "Info", "Warn", "Error", "Title" function DebugPrint($toDump, $level = "Info") { global $debugMode, $debugDump, $debugCount; if ($debugMode != 'N') { $debugDump[$debugCount++] = "<div class='Dbg$level'>" . $toDump . "</div>\n"; } } // Initialize debug information collection $debugMode = 'N'; // N=no, desactivated, P=dump to Web page, F=dump to file $debugSavePath = 'C:\www\App\log_debug.txt'; // If mode F $debugDump = array(); $debugCount = 0; // Primitive debug message dump function DebugDump() { global $debugMode, $debugSavePath, $debugDump, $debugCount; if ($debugMode == 'F') { $fp = fopen($debugSavePath, "a"); #open for writing } if ($debugCount > 0) { switch ($debugMode) { case 'P': echo '<div style="color: red; background: #8FC; font-size: 24px;">Debug:<br /> '; for ($i = 0; $i < $debugCount; $i++) { echo $debugDump[$i]; } echo '</div> '; break; case 'F': for ($i = 0; $i < $debugCount; $i++) { fputs($fp, $debugDump[$i]); } break; //~ default: //~ echo "debugMode = $debugMode<br />\n"; } } if ($fp != null) { fputs($fp, "-----\n"); fclose($fp); } } // Pre array dump function DebugArrayPrint($array) { global $debugMode; if ($debugMode != 'N') { return "<pre class='ArrayPrint'>" . print_r($array, true) . "</pre>"; } else return ""; // Gain some microseconds... } 

The interest is to defer output to the end of the page, avoiding cluttering the real page.

+2
source

If you want to avoid sending errors to the browser, but want the benefits of var_dump and print_r, look at output buffering:

 ob_start(); var_dump($this); echo $that; print_r($stuff); $out = ob_get_contents(); ob_end_clean(); user_error($out); 

good reading at http://www.php.net/manual/en/book.outcontrol.php

+1
source

There is something that I find very useful, that there are not enough standard PHP implementations of variable dump functions, i.e. the ability to simply print part of the expression.

Here is a function that solves this:

 function dump($val) { echo '<pre>'.var_export($val,true).'</pre>'; return $val; } 

Now I can just put the function call around the expression, I need to know the value without interrupting the normal code execution flow as follows:

 $a=2+dump(2*2); 

Using the lesser-known var_export, I also eliminated the need to implement output buffering for subsequent processing of the result.

+1
source

When you create a binary response (i.e. an image using the GD library), you can use the correct custom header:

header('X-eleg:'.serialize($yourstuff));

and use the Http header extension for Firefox to β€œspy” it.

+1
source

print_r () is usually, but var_dump () provides better information for primitives.

In doing so, I do most of my actual debugging using the Zend server debugger.

0
source

var_dump. With XDebug installed, it formats and paints the output nicely (without the need to place <pre> tags around it).

0
source

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


All Articles