← lessons

Create a conditional breakpoint in PhpStorm with Xdebug

Video Lesson

Learn how to set up conditional breakpoints in PhpStorm with Xdebug to pause execution only when specific conditions are met.

Lesson Content

Conditional breakpoints are regular breakpoints with a twist – they only pause execution when specific conditions are met. They're incredibly useful when debugging loops, or when you only care to debug certain scenarios.

Let's use a real-world example. Say we're processing orders and only want to debug those over $100:

<?php

function processOrders(array $orders): void {
    foreach ($orders as $order) {
        // Complex order processing logic here
        if ($order['status'] === 'pending') {
            $total = calculateOrderTotal($order);

            // Additional processing logic...
            updateOrderStatus($order, $total);
        }
    }
}

function calculateOrderTotal(array $order): float {
    $total = 0;
    foreach ($order['items'] as $item) {
        $total += $item['price'] * $item['quantity'];
    }
    return $total;
}

function updateOrderStatus(array &$order, float $total): void {
    // In a real app, this would update the order in a database
    // but for this example, we'll keep it simple
    $order['total'] = $total;
    $order['status'] = 'processed';
}

// Example usage with multiple orders
$orders = [
    [
        'id' => 1,
        'status' => 'pending',
        'items' => [
            ['price' => 10.00, 'quantity' => 2],
            ['price' => 25.50, 'quantity' => 1]
        ]
    ],
    [
        'id' => 2,
        'status' => 'pending',
        'items' => [
            ['price' => 100.00, 'quantity' => 1],
            ['price' => 50.00, 'quantity' => 3]
        ]
    ]
];

processOrders($orders);

We want to set a breakpoint in the processOrders function on the line updateOrderStatus($order, $total);

Here's how to set up your conditional breakpoint:

  1. Click in the left gutter to create a regular breakpoint

  2. Right-click the red breakpoint dot

  3. Enter $total > 100 in the "Condition" field

That's it! The debugger will now only pause when an order's total exceeds $100.

PhpStorm lets you use any valid PHP expression as your condition, with a few limitations:

  • Keep expressions to a single line

  • No variable assignments

  • Simple expressions work best

Here are some practical examples you might find useful:

$order['id'] === 2               // Break on a specific order
in_array($order['id'], [2, 3])   // Break on multiple orders
count($order['items']) > 3       // Break when orders have lots of items