Archived video

This video is archived and won't be updated. We're keeping it online for now as a courtesy.

Debugging in PhpStorm

Video loading...

  • 0:04
    PhpStorm Video Tutorial Debugging in PhpStorm
  • 0:12
    With PhpStorm, it's possible to debug our PHP codes,
  • 0:17
    whether it's running from the console
  • 0:18
    or on a local or remote web server.
  • 0:21
    Two popular debuggers are Xdebug and Zend Debugger,
  • 0:25
    which are both supported by PhpStorm.
  • 0:28
    Before being able to debug, we must configure our PHP
  • 0:31
    installation to make use of one of them.
  • 0:34
    Do note that it's not possible to use them simultaneously
  • 0:37
    as they will block each other.
  • 0:40
    Let's first have a look at how to configure Xdebug.
  • 0:44
    First of all, we have to download Xdebug from the website.
  • 0:48
    The Downloads page lists Xdebug versions for all versions of PHP
  • 0:52
    on different platforms.
  • 0:54
    Find the one that matches your PHP version
  • 0:56
    and extract it to your PHP extension directory.
  • 1:00
    If you are unsure about which version to download,
  • 1:03
    the wizard at xdebug.org/wizard.php helps finding the correct version
  • 1:08
    based on our local PHP configuration.
  • 1:13
    Next, we have to configure Xdebug.
  • 1:16
    Open php.ini and add the required configuration.
  • 1:20
    The web help contains some examples for that.
  • 1:26
    We've now seen how to configure Xdebug.
  • 1:29
    Zend Debugger is also supported by PhpStorm,
  • 1:32
    so let's see how we can configure that one instead of Xdebug.
  • 1:36
    From Zend's website, download the Zend Debugger package
  • 1:40
    and extract it to a location on your system.
  • 1:43
    Next, edit your php.ini file and add the required configuration.
  • 1:48
    Again, the web help contains some examples to help you out.
  • 1:54
    Now that we have our configuration ready, let's start debugging.
  • 1:59
    Every project can have its own debugging settings.
  • 2:02
    From the project settings, we can configure PhpStorm to make
  • 2:06
    use of Xdebug, Zend Debugger, specify
  • 2:09
    additional configuration options, and so on.
  • 2:12
    If we wanted to connect to an Xdebug proxy, which forwards debugger
  • 2:16
    connections to a remote server, we can also configure that here.
  • 2:20
    But let's just go with the defaults.
  • 2:25
    We can create a new Run configuration
  • 2:27
    for a file we want to debug.
  • 2:29
    This Run configuration does not require any specific settings
  • 2:33
    for a debugger to work except when we
  • 2:35
    want to debug on a remote server.
  • 2:38
    Note there's also a configuration type,
  • 2:40
    which allows us to issue an HTTP request against a server
  • 2:44
    and attach the debugger to that server for only that HTTP request.
  • 2:48
    But let's just go with a simple configuration,
  • 2:51
    which runs our PHP scripts.
  • 2:56
    In the editor, we can set breakpoints.
  • 3:00
    Breakpoints are lines of code in which a debugger will pause
  • 3:03
    execution and allow us to see what's going on inside our code.
  • 3:07
    We can set breakpoints by clicking the gutter at the specific line
  • 3:10
    of code or by placing the cursor on a line of code
  • 3:14
    and using the keyboard shortcuts to toggle the breakpoints.
  • 3:18
    Let's add some breakpoints and start the debugger
  • 3:20
    by clicking the Debugger icon next to our Run configuration
  • 3:24
    or by using the keyboard shortcuts.
  • 3:29
    Once the debugger hits the first breakpoint,
  • 3:32
    a new tool window is opened.
  • 3:34
    From here, we can navigate through the running code
  • 3:36
    and inspect variables.
  • 3:39
    Using the toolbar buttons or the keyboard shortcuts,
  • 3:42
    we can step through code.
  • 3:44
    The Step Into function follows every function call.
  • 3:48
    Step Over just executes the function call
  • 3:50
    and keeps us in the current piece of code.
  • 3:54
    In the loop we have in our code, the breakpoint
  • 3:57
    is hit in every iteration of the loop.
  • 3:59
    If we are only interested in seeing what's
  • 4:01
    going on in our code for a specific iteration,
  • 4:04
    we can make the breakpoint a conditional breakpoint.
  • 4:08
    Using Run, View Breakpoints or the keyboard shortcuts,
  • 4:12
    we can specify the condition when the breakpoint is valid.
  • 4:16
    Note that we can also disable the breakpoint
  • 4:18
    until another breakpoint has been hit.
  • 4:22
    Let's run it again.
  • 4:24
    As you can see, the breakpoint is now only hit for the last element
  • 4:27
    of our loop, which matches the condition we specified earlier.
  • 4:34
    Another interesting debugging feature are Watches.
  • 4:37
    While debugging, variables are shown that are
  • 4:39
    valid in the current execution context.
  • 4:42
    Unfortunately, that list can grow very, very long.
  • 4:46
    Wouldn't it be nice if we could pin a list of variables
  • 4:49
    so we can see them easily?
  • 4:51
    Well, that's exactly what Watches allow us to do.
  • 4:54
    Let's pin a variable there.
  • 5:01
    While debugging, it may be useful to be
  • 5:04
    able to run additional statements on that code.
  • 5:07
    For example, we can use the evaluate expression feature
  • 5:11
    and run any expression on the current scope.
  • 5:14
    Let's print out the names array.
  • 5:21
    We can also influence execution.
  • 5:23
    For example, we can set the current name to a different value.
  • 5:28
    Note that we can also do this from the variables view.
  • 5:31
    Press F2 and simply change the value of running variables.
  • 5:37
    So far, we've only been debugging a simple script.
  • 5:40
    Once PHP has been configured, we can also
  • 5:43
    make use of a Run configuration which runs on the server.
  • 5:47
    On my screen here, you'll see a configuration
  • 5:49
    where I'm making use of a local server
  • 5:51
    to run the exact same script we've used before.
  • 5:55
    If we now start this configuration on the server,
  • 5:58
    we can debug the script as well.
Loading ...

Debugging in PhpStorm

Loading...

One of the great advantages of an IDE over a text editor is the ability to easily run a debugger. In this lesson you'll learn how to configure PhpStorm to use XDebug. We'll walk through getting XDebug set up, and then how to debug, including setting breakpoints.

Note: From the menu bar, PhpStorm > Preferences > PHP > Debug will apply settings to the current project as described in the video.

Additional resources: 

XDebug

Please note that this series covers PhpStorm versions 6 and 7 only.

For the latest documentation (including up-to-date videos), see JetBrains documentation.