Debugging
Debugging is the work of asking one precise question at a time. In PHP two tools answer most of them: a dump of a value and a line in the error log.
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.
<?php $user = ["id" => 7, "name" => "Ada", "roles" => ["admin"]]; echo "USER:", PHP_EOL; var_dump($user["id"], $user["name"]); exit;
php dump.php
USER:
int(7)
string(3) "Ada"
var_dump() prints the type next to the value, so an empty string and a zero cannot be confused. The label says which question the dump answers, and exit stops the rest of the page from adding noise.
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.
<?php $orderId = 42; error_log("Order paid: " . $orderId); echo "order handled", PHP_EOL;
php log.php
order handled
Only the confirmation reaches the body; the logged line leaves through the error channel and lands in the log of the server. A reader of the page never sees the order id, while the operator can find it later.
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.
Exercises
- Dump the whole $user array instead of two keys and read the indentation of the output.
- Move the exit above the dump and describe what the page shows.
- Send the logged line of log.php to a file with the error_log ini setting and open it.
- Take a bug you know and write the smallest steps that trigger it, before any change.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
var_dump()show thatechodoes not?Answer
The type and the structure of the value
The dump prints the type and every element, which is what separates an empty string from a zero.
-
Where does
error_log()write?Answer
Into the PHP error log
The message leaves through the error channel, so the body of the page stays clean.
-
What is the first step of a debugging session?
Answer
Reproduce the bug with the smallest steps
A bug that cannot be triggered on demand cannot be checked after a fix either.
-
Why must every debug line be removed before the code ships?
Answer
It leaks internals and breaks the layout of the page
The dump was written for you, not for a visitor, and it exposes values that belong to the server.
-
What does this program print?
<?php var_dump(7);Answer
int(7)
The dump shows the integer type and the value in parentheses.
-
What does this program print?
<?php var_dump("Ada");Answer
string(3) "Ada"
A string dump names the type, counts its characters and quotes the value.
-
What does this program print?
<?php $user = ["id" => 7]; var_dump(array_key_exists("id", $user));Answer
bool(true)
The key exists in the array, so the check returns true.
-
What does this program print?
<?php var_dump(null);Answer
NULL
A null value dumps as the word NULL and nothing else.
-
What is wrong with leaving
var_dump($user); die;in a production page?Answer
It shows private values to every visitor and stops the page
The dump prints data the visitor should never see, and the die call cuts the page after it.
-
What does
exitafter a dump do?Answer
It stops the script, so nothing after it runs
The call ends the request, which is why the dump stands alone in the output.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev