Debugging
| var_dump that helps | |
| error_log | |
| Reproduce first | |
| A debugging checklist |
var_dump that helps
A dump answers one question: what is in this variable right here. Print a label with it, dump and die once, and remove the line after. Dumps left in production code leak internals to users.
echo "USER:"; var_dump($user); exit;
error_log
Some bugs only happen where you cannot watch - cron jobs, webhooks, production. error_log() writes to the PHP log instead of the screen, with no layout to break and no secrets shown to users.
error_log("Order paid: " . $orderId);
Reproduce first
Before changing anything, write the smallest steps that trigger the bug every time. If it is not reproducible, it is not understood - and a fix for a mystery is a guess. Each story in PHP errors: case studies starts exactly this way.
A debugging checklist
1. Reproduce the bug with the smallest possible steps.
2. Narrow the area: dump inputs and outputs at each boundary.
3. Form one hypothesis and test it with a log line, not a rewrite.
4. Fix, re-run the reproduction steps, then remove every debug line.
Next: Modern PHP tour: 5 to 8.4
Article author: Arthur Isaev