Skip to end of metadata
Go to start of metadata

 

When building web applications with multiple tiers, we can have a situation in which frontend PHP code calls into backend PHP code. Often, developers work on both sides of such application at the same time, but how can we debug them simultaneously? How can we step from front-end code into back-end code? In this tutorial, we'll look at a number of ways to do this.

Icon

Be sure to check the other tutorials about debugging PHP code with PhpStorm to learn more.

Prerequisites

To debug PHP code with PhpStorm, we will need Xdebug or Zend Debugger. Make sure either Xdebug or Zend Debugger are installed and configured with PhpStorm.

1. Ensure a debugger session is started for child requests

The way Xdebug and Zend Debugger work is that they make a connection from the PHP interpreter to our IDE. While possible, it's good practice to not do this for every request being made but only for those where debugging is desired. For our frontend, we'll do this from the browser using the PhpStorm bookmarklets or a Browser Debugging Extension.

Both debugging engines can start a debugging session on-demand by passing in a request variable (XDEBUG_SESSION_START=session_name for Xdebug, start_debug=1 for Zend Debugger), which is what the bookmarklets and browser extensions do. However, this will only work for the first script we hit. Consider the following two scripts, frontend.php and backend.php.

frontend.php
backend.php

When the first script calls into the second script (using file_get_contents), it starts a separate HTTP connection which is not part of an existing debugging session nor starts a new one. If we want Xdebug or Zend Debugger to start debugging for the called script as well, we'll have to either configure the debugger to start debugging all the time (xdebug.remote_autostart=1 for Xdebug), or pass the request parameters along.

Let's rewrite the frontend script to do this. In its simplest form, we could check for the Xdebug or Zend Debugger request parameters to be present and just pass them along.

frontend.php

This should start the client debugging session with the same session name as the parent (unless Xdebug was started using cookies, where we fallback to using the default PHPSTORM session name).

2. Increase the number of simultaneous debugger connections

By default, PhpStorm accepts only one debugger connection at a time. This is a good thing: imagine making extensive use of AJAX and having a debugging session per AJAX call that comes in! For some scenarios, it makes sense to allow more than one connection though, for example for debugging a frontend and backend at the same time.

We can enable support for having more than one debugger connection for our project. In the settings / preferences (Ctrl+Alt+S / CMD+,), expand the Languages & Frameworks | PHP | Debug node and increase the maximum number of connections, for example to 3.

3. Listen for incoming debugger connections

From the toolbar, toggle the “Listen debugger connections” button (or use the Run | Start Listening for PHP Debug Connections menu). This will ensure PhpStorm reacts when a debugging session is started in our application.

4. Start a debugging session

Using the PhpStorm bookmarklets or a Browser Debugging Extension, we can start a debugging session from the browser. If we place a breakpoint in our frontend script, PhpStorm will notify us that there's an incoming debugger connection and pause script execution.

If we step into the file_get_contents function call, PHP will start a second debugger connection, which in turn opens the debugger in PhpStorm (in a separate tab). We've now effectively stepped from our frontend to the backend.

When the backend script returns, the debugger continues where we left in the frontend script.

(optional) Simultaneous Debugging of PHP and JavaScript

Web applications typically consist of both PHP and JavaScript code. PHP code will run on the server side, JavaScript will run in the browser. With PhpStorm, we can easily debug the PHP code to inspect what is happening on the server, modify variables and so on. We can also debug the JavaScript running in the browser by starting a JavaScript debugging session from our IDE. We can debug both parts at the same time!

Check the Debugging PHP and JavaScript code at the same time in PhpStorm tutorial to learn more about how to do this.

  • No labels