|
| 1 | +--- |
| 2 | +title: Functions |
| 3 | +--- |
| 4 | + |
| 5 | +PHP Debugger defines five functions, listed here in alphabetical order. |
| 6 | + |
| 7 | +All of them are safe to call whether or not a debugging session is running — with |
| 8 | +nothing connected they do nothing and report that they did nothing, rather than |
| 9 | +failing. |
| 10 | + |
| 11 | +## `php_debugger_break` |
| 12 | + |
| 13 | +```php |
| 14 | +php_debugger_break(): bool |
| 15 | +``` |
| 16 | + |
| 17 | +| | | |
| 18 | +| --- | --- | |
| 19 | +| Parameters | None | |
| 20 | +| Returns | `true` if execution stopped, `false` if it could not | |
| 21 | + |
| 22 | +Stops execution at this line and hands the session to your editor, exactly as |
| 23 | +though you had set a [breakpoint](../user-guide/breakpoints.md) on it. |
| 24 | + |
| 25 | +Useful where there is no line to click on: generated code, a template compiled at |
| 26 | +runtime, or a file your editor cannot map to what is really executing. |
| 27 | + |
| 28 | +It needs somewhere to stop to. With your editor connected it just works. With |
| 29 | +nothing connected it returns `false` and raises a notice, unless |
| 30 | +[`php_debugger.on_demand_debugging_enabled`](./settings.md#php_debuggeron_demand_debugging_enabled) |
| 31 | +is set, which lets the debugger attach mid-request. |
| 32 | + |
| 33 | +```php |
| 34 | +$template = $this->compile($source); |
| 35 | + |
| 36 | +if ($template === '') { |
| 37 | + php_debugger_break(); |
| 38 | +} |
| 39 | + |
| 40 | +eval($template); |
| 41 | +``` |
| 42 | + |
| 43 | +Being real code, it goes into a commit if you are not careful. Prefer a breakpoint |
| 44 | +set from your editor whenever one will do. |
| 45 | + |
| 46 | +## `php_debugger_connect_to_client` |
| 47 | + |
| 48 | +```php |
| 49 | +php_debugger_connect_to_client(): bool |
| 50 | +``` |
| 51 | + |
| 52 | +| | | |
| 53 | +| --- | --- | |
| 54 | +| Parameters | None | |
| 55 | +| Returns | `true` if the connection was set up, `false` otherwise | |
| 56 | + |
| 57 | +Connects to your editor part-way through a request, for the case where the session |
| 58 | +did not start at the beginning of it. |
| 59 | + |
| 60 | +This only works with |
| 61 | +[`php_debugger.on_demand_debugging_enabled`](./settings.md#php_debuggeron_demand_debugging_enabled) |
| 62 | +turned on. Without it the call returns `false` and raises a notice, because a |
| 63 | +request that was not compiled with debugging instrumentation cannot be debugged |
| 64 | +part-way through. |
| 65 | + |
| 66 | +```php |
| 67 | +if ($order->getTotal() < 0) { |
| 68 | + php_debugger_connect_to_client(); |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +Unlike `php_debugger_break()`, this connects without stopping. Execution carries on |
| 73 | +until something else — a breakpoint, or a later `php_debugger_break()` — pauses it. |
| 74 | + |
| 75 | +## `php_debugger_info` |
| 76 | + |
| 77 | +```php |
| 78 | +php_debugger_info(?string $category = null): mixed |
| 79 | +``` |
| 80 | + |
| 81 | +| | | |
| 82 | +| --- | --- | |
| 83 | +| `$category` | `null` for the full report, or `"mode"` or `"extension-flags"` | |
| 84 | +| Returns | `null` for the full report, which it prints; an array for a category | |
| 85 | + |
| 86 | +Reports how the debugger is configured and what it is currently doing. |
| 87 | + |
| 88 | +Called with no argument it prints a page — HTML under a web server, plain text on |
| 89 | +the command line — covering whether the debugger is loaded and active, which client |
| 90 | +it connected to, and every setting's value. It also includes a **Diagnostic Log** |
| 91 | +section listing the warnings and errors raised during this request, which makes it |
| 92 | +the first thing to reach for when something is not working. |
| 93 | + |
| 94 | +```php |
| 95 | +php_debugger_info(); |
| 96 | +``` |
| 97 | + |
| 98 | +With a category it returns an array instead of printing: |
| 99 | + |
| 100 | +```php |
| 101 | +php_debugger_info('mode'); |
| 102 | +// ['debug'] |
| 103 | + |
| 104 | +php_debugger_info('extension-flags'); |
| 105 | +// ['control-socket'] |
| 106 | +``` |
| 107 | + |
| 108 | +`mode` gives the modes currently active, and `extension-flags` the optional |
| 109 | +features this build was compiled with. |
| 110 | + |
| 111 | +## `php_debugger_is_debugger_active` |
| 112 | + |
| 113 | +```php |
| 114 | +php_debugger_is_debugger_active(): bool |
| 115 | +``` |
| 116 | + |
| 117 | +| | | |
| 118 | +| --- | --- | |
| 119 | +| Parameters | None | |
| 120 | +| Returns | `true` if a debugging session is connected right now | |
| 121 | + |
| 122 | +Reports whether a client is connected. It has no side effects and never raises |
| 123 | +anything, so it is the safe way to ask before doing something that only makes sense |
| 124 | +while debugging. |
| 125 | + |
| 126 | +```php |
| 127 | +if (php_debugger_is_debugger_active()) { |
| 128 | + $timeout = 0; |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +Guarding a long timeout, as above, is one example: stepping through code takes far |
| 133 | +longer than any real request, and without a guard like that you spend your session |
| 134 | +watching things expire. |
| 135 | + |
| 136 | +## `php_debugger_notify` |
| 137 | + |
| 138 | +```php |
| 139 | +php_debugger_notify(mixed $data): bool |
| 140 | +``` |
| 141 | + |
| 142 | +| | | |
| 143 | +| --- | --- | |
| 144 | +| `$data` | Any value. Sent to your editor as a structured value, not a string | |
| 145 | +| Returns | `true` if the notification was sent, `false` if nothing is connected | |
| 146 | + |
| 147 | +Sends a value to your editor's notification panel, along with the file and line it |
| 148 | +came from, without stopping execution. |
| 149 | + |
| 150 | +It is `var_dump()` that goes to your editor instead of into the response — useful |
| 151 | +where writing to the output would corrupt it, or where you want a record of what a |
| 152 | +loop did without stopping on every iteration. |
| 153 | + |
| 154 | +```php |
| 155 | +foreach ($rows as $i => $row) { |
| 156 | + if ($row->isInvalid()) { |
| 157 | + php_debugger_notify(['index' => $i, 'row' => $row]); |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +Because the value is sent structured rather than flattened to a string, arrays and |
| 163 | +objects arrive expandable in your editor, the same as anything in the variables |
| 164 | +panel. |
| 165 | + |
| 166 | +With no session connected it returns `false` and does nothing else, so calls left |
| 167 | +in place cost almost nothing. |
| 168 | + |
| 169 | +## The `xdebug_` prefix |
| 170 | + |
| 171 | +Every function above also exists with an `xdebug_` prefix — `xdebug_break()`, |
| 172 | +`xdebug_connect_to_client()`, `xdebug_info()`, `xdebug_is_debugger_active()` and |
| 173 | +`xdebug_notify()`. The two spellings are the same function, so code written against |
| 174 | +either name works unchanged. |
| 175 | + |
| 176 | +The `php_debugger_` names are the ones to prefer in new code. |
| 177 | + |
| 178 | +:::info[Functions from removed features] |
| 179 | + |
| 180 | +Xdebug's other functions — the ones belonging to profiling, tracing, code coverage, |
| 181 | +garbage collection statistics and the development helpers — are still defined, so |
| 182 | +code that calls them will not fail with an undefined function error. |
| 183 | + |
| 184 | +They do nothing. Each raises a deprecation notice saying which feature was removed |
| 185 | +and returns a harmless value, so a call left behind in an old codebase is a message |
| 186 | +in your log rather than a crash. |
| 187 | + |
| 188 | +::: |
0 commit comments