Gosu.pl

PHP debug tools

Download

Version: 1.03: debug.zip [276 KB] [2009-12-29] (see changelog)
License: BSD revised (free for any use)
Author: Cezary Tomczak ()
Subscribe to new releases: http://freshmeat.net/projects/php-debug-tools/

Important note - xdebug trace script (example 1)

2010-05-19: Xdebug trace script (example 1 popup) has been tested fully with xdebug 2.0.4. I've heard that there has been some major changes in 2.1.x version to how the script trace is saved. Seems like the xdebug-trace.php script doesn't work with the new version. I'm going to release an update soon, when I find some time to update my xdebug version and the trace script. In the meantime, if you want to use the script trace feature, try installing version 2.0.4 and it will work fine even with the latest php 5.3, just the code for the xdebug 2.1 is outdated.

Examples

  1. Debug script trace (dependency: xdebug php extension)
  2. Debug errors
  3. Debug using debug() function
  4. Debug using dump(), dump_tofile() functions
  5. Debug database queries

Example 1: debug script trace

To use trace debugging script you need to:

  1. Install xdebug php extension.
  2. chmod /dev/data/ directory so it is writable.

Xdebug install

How to install docs: http://www.xdebug.org/docs/install

Example installation (for windows, tested with xdebug version 2.0.4, php version 5.2.8, nts non-thread safe):

  1. Download xdebug from: http://www.xdebug.org/download.php
  2. Copy dll to php extensions directory, for example: C:/php/ext/php_xdebug-2.0.4-5.2.8-nts.dll
  3. Add this directives to php.ini:
    zend_extension = "C:/php/ext/php_xdebug-2.0.4-5.2.8-nts.dll"
    xdebug.collect_includes = Off
    xdebug.default_enable = Off
    xdebug.dump_globals = Off
    xdebug.dump_once = Off
    xdebug.extended_info = Off
    
    This example is for non-thread safe version. For the thread safe version change "zend_extension" to "zend_extension_ts".

Input

<?php
include './auto_prepend.php';
echo '<h1>test3: trace</h1>';
function test1()
{
    test2();
}
function test2()
{
    for ($i 0$i 100$i++) {
        $s substr(''01);
    }
}
test1();
include './auto_append.php';
?>

Output

In the bottom-right corner will be displayed a debug console, which shows execution time of php script along with used memory. The "start" link enables xdebug session, after the session is enabled you have to click "xdebug-trace" link to view the script trace in a popup window.

Step 1 - click "start" to enable xdebug session

php debug tools - trace example - debug console start

Step 2 - click "xdebug-trace" to open popup window

php debug tools - trace example - debug console open popup window

Step 3 - xdebug-trace popup window

php debug tools - xdebug trace popup window

Including debug console in all scripts on localhost

You can set php.ini directives "auto_prepend_file" & "auto_append_file" to point to auto_prepend.php & auto_append.php files, and you will see the debug console in each script that you are running on your localhost machine.

Posssible issues when including via php.ini:

If your scripts do some ajax requests, there might be some issues, because debug console might be displayed more than once, to avoid it, some modifications to auto_prepend.php & auto_append.php might be required.

A strange behavior might appear (starting/stopping xdebug session might not work) if you're sending ETag headers and including auto_prepend.php through php.ini. It happens because debug console's html is sent after the script execution, so the content is different then the ETag suggests. A simple modification in your script can fix that problem, just don't send ETags on localhost.

Example 2: debug errors

Input

<?php
require './lib/debug.php';
function test($a$b)
{
    echo $asd;
}
test(10'abc');
?>

Output

php debug tools - debug errors

Example 3: debug using debug() function

Input

<?php
require './lib/debug.php';
function test($args)
{
    test_nested($args);
}
function test_nested($args)
{
    debug($args);
    // or: debug(get_defined_vars());
    // or: debug();
}
test(array('id'=>123'str'=>'test'));
?>

Output

php debug tools - debug using debug() function

Example 4: debug using dump(), dump_tofile() functions

This function is a light version of debug(), with just basic features and a light interface.

Input

<?php

include_once './lib/dump.php';

function test5()
{
    include './testdata/test0.php';
    $test = array('int'=>1'float'=>2.0'float2'=>2.1);
    dump($test$_SERVER);
}
function test1() { test2(); }
function test2() { test3(); }
function test3() { test4(); }
function test4() { test5(); }

test1();

?>

Output

php debug tools - debug using dump(), dump_tofile() functions

dump_tofile()

This function is useful while debugging when:

Example: debug/test7-dump_tofile.php

Example 5: debug database queries

Note: before proceeding chmod /dev/data/ directory so it is writable.

Input

<?php
include './auto_prepend.php';

include './lib/db-mysql.php';
db_connect(array(
    'host' => 'localhost',
    'user' => 'root',
    'pass' => 'toor',
    'dbname' => 'test',
    'charset' => 'latin2'
));
db_query('CREATE TABLE IF NOT EXISTS testdebug (id int PRIMARY KEY)');
db_query('DELETE FROM testdebug WHERE 1=1');
db_insert('testdebug', array('id'=>1));
db_insert('testdebug', array('id'=>2));
db_insert('testdebug', array('id'=>3));
$id1 db_one('SELECT id FROM testdebug WHERE id = %id', array('id'=>1));
$row_id2 db_row('SELECT * FROM testdebug WHERE id = %0', array(1));
$assoc db_assoc('SELECT id, id FROM testdebug ORDER BY id');

include './auto_append.php';
?>

Output

Step1 - click "db-debug (10)" to open popup window

php debug tools - debug database queries - debug console

Step2 - db-debug popup window

php debug tools - debug database queries - db-debug popup window

Input - mysql error

<?php
require './auto_prepend.php';

include './lib/db-mysql.php';
db_connect(array(
    'host' => 'localhost',
    'user' => 'root',
    'pass' => 'toor',
    'dbname' => 'test',
    'charset' => 'latin2'
));
function test()
{
    db_query('CREATE TABLE asd (id int PRIMARYYYY KEY)');
}
test();

require './auto_append.php';
?>

Output - mysql error

php debug tools - debug database queries - mysql error

Changelog

1.03

1.02

1.01