Bookmark and Share

Remote debugging PHP using Xdebug on MAMP

Posted: Thursday, May 21st, 2009 at 10:19 pmUpdated: Thursday, May 28th, 2009 at 5:38 pm

Now that we verify that Xdebug is properly installed in our MAMP stack, let’s configure it for remote debugging. Edit your php.ini again and add the following configuration options. Save the modified php.ini and restart your Apache server.

xdebug.remote_enable   = On
xdebug.remote_host     = "localhost"
xdebug.remote_port     = 9000
xdebug.remote_handler  = "dbgp"
xdebug.profiler_enable = On

Since we’ll be using Eclipse IDE, we better have Eclipse installed with PHP support. Please visit Eclipse PDT site to install Eclipse and configure PHP support. From this point on, I’ll assume you have Eclipse working with PHP development support.

Using Eclipse IDE for PHP remote debugging

The best tutorial, I think is by example. In this tutorial, we’ll start from creating a project to writing and start debugging. So once Eclipse IDE is opened, click on File -> New -> PHP Project. Since I’m using MAMP app, I’m setting the project location to /Applications/MAMP/htdocs. Click finish button.
eclipse-remote-debug-tutorial

Now, making sure eclipse remote debugging tutorial project selected, create new PHP file by clicking on File -> New -> PHP File. Name the file as test_debug.php. Click on finish button.
eclipse-remote-debug-create-file

Then type in the following PHP code and save test_debug.php.

<?php
function divide($num, $divisor) {
	if (($divisor % 2) == 0) {
		return 0;
	}
	$ret_val = $num / $divisor;

	return $ret_val;
}

for ($i = 0; $i < 5; $i++) {
	$divided = divide($i + 3, $i);
	echo "Iteration $i divided is $divided <br />";
}

?>

And the expected result if you run the script above is as follows:
test_debugphp-expected-result

Next, we’ll prepare Eclipse IDE for remote debugging.

Pages: 1 2 3 4

Leave a Reply

Security Code: