PHP 7.1.24 Released

D├ębogueur PHP

Sommaire

add a note add a note

User Contributed Notes 7 notes

up
118
netyou
3 years ago
I find it very useful to print out to the browsers console instead of just var_dumping:

function console_log( $data ){
  echo '<script>';
  echo 'console.log('. json_encode( $data ) .')';
  echo '</script>';
}

Usage:
$myvar = array(1,2,3);
console_log( $myvar ); // [1,2,3]
up
3
eric at derewonko dot com
9 months ago
None of the given examples here do fit my needs.
When I debug, I want to see clearly what's going on and where is the debug data. Also it has to accommodate with the current styles and not fall behind or get mixed with other text etc.
This snippet will give you real debug notices in a flashy table:
- complete backtrace of calls on the left with line numbers
- arguments content on the right, with the variable name and coloring per variable type

<?php
function wtf(){
 
error_reporting(E_ALL);
 
$args = func_get_args();
 
$backtrace = debug_backtrace();
 
$file = file($backtrace[0]['file']);
 
$src  = $file[$backtrace[0]['line']-1];  // select debug($varname) line where it has been called
 
$pat  = '#(.*)'.__FUNCTION__.' *?\( *?\$(.*) *?\)(.*)#i'// search pattern for wtf(parameter)
 
$arguments  = trim(preg_replace($pat, '$2', $src));  // extract arguments pattern
 
$args_arr = array_map('trim', explode(',', $arguments));

  print
'<style>
  div.debug {visible; clear: both; display: table; width: 100%; font-family: Courier,monospace; border: medium solid red; background-color: yellow; border-spacing: 5px; z-index: 999;}
  div.debug > div {display: unset; margin: 5px; border-spacing: 5px; padding: 5px;}
  div.debug .cell {display: inline-flex; padding: 5px; white-space: pre-wrap;}
  div.debug .left-cell {float: left; background-color: Violet;}
  div.debug .array {color: RebeccaPurple; background-color: Violet;}
  div.debug .object pre {color: DodgerBlue; background-color: PowderBlue;}
  div.debug .variable pre {color: RebeccaPurple; background-color: LightGoldenRodYellow;}
  div.debug pre {white-space: pre-wrap;}
  </style>'
.PHP_EOL;
  print
'<div class="debug">'.PHP_EOL;
  foreach (
$args as $key => $arg) {
    print
'<div><div class="left-cell cell"><b>';
   
array_walk(debug_backtrace(),create_function('$a,$b','print "{$a[\'function\']}()(".basename($a[\'file\']).":{$a[\'line\']})<br> ";'));
    print
'</b></div>'.PHP_EOL;
    if (
is_array($arg)) {
      print
'<div class="cell array"><b>'.$args_arr[$key].' = </b>';
     
print_r(htmlspecialchars(print_r($arg)), ENT_COMPAT, 'UTF-8');
      print
'</div>'.PHP_EOL;
    } elseif (
is_object($arg)) {
      print
'<div class="cell object"><pre><b>'.$args_arr[$key].' = </b>';
     
print_r(htmlspecialchars(print_r(var_dump($arg))), ENT_COMPAT, 'UTF-8');
      print
'</pre></div>'.PHP_EOL;
    } else {
      print
'<div class="cell variable"><pre><b>'.$args_arr[$key].' = </b>&gt;';
     
print_r(htmlspecialchars($arg, ENT_COMPAT, 'UTF-8').'&lt;');
      print
'</pre></div>'.PHP_EOL;
    }
    print
'</div>'.PHP_EOL;
  }
  print
'</div>'.PHP_EOL;
}
?>

Usage: wtf($arg1, $arg2,..);
That's it!
up
3
Anonymous
1 year ago
I would like to add http://phpdebugbar.com/ which is also a handy tool for debugging/profiling data. and you could use it whenever framework you are using. :-)
up
4
online _ use _ only == hotmail.com
12 years ago
I still find that printing out variable values at problem points in the code is one of the easiest ways for me to debug.  If you're interested in knowing the full contents of an object/array/scalar, then use

var_dump($var).
up
-1
rajendra at skydevelopers dot net
1 month ago
I also use a method to print complete data from www.skydevelopers.net company:

function dd($data) {
    echo '<pre>';
    print_r($data);
    exit;
}

well this is also very beneficial
up
-11
skatebiker at gmail dot com
3 years ago
p_r() is a function for logging variable values.

In this example the function p_r() does only log when the URL parameter d=<nonzero> is set. Reset it by d=0.
When  the parameter is a valid filename (relative to the script's path) it will be logged to that file rather than to the browser.

[code]
@session_start();
// debug
if (isset($_GET['d']))
{
  $_SESSION['d'] = $_GET['d'];
}

if (@$_SESSION['d']) {

function p_r($exp)
{
  $res = "";
  $trace = debug_backtrace();
  $level = 0;
  $e = error_reporting(E_ALL&~E_NOTICE);
  $file = strrpos($trace[$level]['file'], "/");
  $file = substr($trace[$level]['file'],$file+1);
  $line = date("H:i:s"). " " . $file . ' ' . $trace[$level]['line'] . ' ' . $trace[$level+1]['function'] . "()";
  $e = error_reporting($e);
  if (!is_string($exp)) $exp = var_export($exp,1);
  if (substr($_SESSION["d"],-4)==".log") {
    file_put_contents ($_SESSION["d"],$line . ": ". $exp . "\n",  FILE_APPEND);
  } else {
    $res = $line . "\n<pre>".htmlentities($exp). "</pre>\n";
    echo $res;
  }
  return $res;
}

    // refresh to prevent timeout
  $a = $_SESSION['d'];
  $_SESSION['d'] = $a;
  error_reporting (E_ALL);

} else {
  function  p_r() {}
}  // end if debug

[/code]
up
-9
Bladimir
1 year ago
Following example above using variable-lenght arguments to support similar Web API's console.log implementation

<?php
function console_log( ...$messages ){
 
$msgs = '';
  foreach (
$messages as $msg) {
   
$msgs .= json_encode($msg);
  }

  echo
'<script>';
  echo
'console.log('. json_encode($msgs) .')';
  echo
'</script>';
}

?>
To Top