Skip to content

Commit c0747bf

Browse files
Merge pull request #23 from php-debugger/functions-reference
write the functions reference page
2 parents a540bf7 + 32bea8c commit c0747bf

2 files changed

Lines changed: 184 additions & 0 deletions

File tree

docs/reference/functions.md

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

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const sidebars = {
4848
collapsed: false,
4949
items: [
5050
'reference/settings',
51+
'reference/functions',
5152
'reference/cli-options',
5253
'reference/environment-variables',
5354
'reference/debug-protocol',

0 commit comments

Comments
 (0)