My favorites | Sign in
Project Home Downloads Issues
READ-ONLY: This project has been archived. For more information see this post.
Project Information
Members

PHP Debug Tools

Download

Version: 1.03: phpdebugtools_1.03.zip (276 KB) (2009-12-29)

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('', 0, 1);
	}
}
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

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

Step 3 - 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

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

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

dump_tofile()

This function is useful while debugging when:

  • you don't want to stop the script execution.
  • you can't display data, because for example it is an ajax request.
  • you want to debug in more than one place through the request.

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

Step2 - 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

Changelog

1.03

  • Fix to dump(): function did not output, when called after a call to dump_tofile()

1.02

  • New tool: lib/dump.php with dump() and dump_tofile() functions.

1.01

  • changes in auto_prepend.php: invalid path to xdebug-trace script when including from different directory (running tests worked, but when including for example through php.ini, then paths were invalid), this also applies to db-debug script.
  • problems when including debug console via php.ini: the console was displayed also in the xdebug trace popup window, so the original xdebug session was overwritten (console is now disabled for the scripts in /dev/ by checking name of the php file, so these files names cannot be changed: xdebug-trace.php, db-debug.php, db-debug-analyze.php)
  • fixed short open tags in php files (scripts in /dev/ now are working when short_open_tag directive is off in php.ini)
  • changes in lib/debug.php: possible E_NOTICE errors if you had global variables named $db or $Db, but keys debug_time/debug_queries were missing in these arrays.

Powered by Google Project Hosting