In this chapter
We'll learn PHP's three tools for looking inside a value — var_dump(), print_r() and var_export() — what each one shows, when to use which, and the simple debugging habit every developer uses: look at the real value instead of guessing.
The Problem in Real Life
This week Mia met three values that lied to her. $isOpen printed nothing. A price that looked like 25 turned out to be text. And when she tried to print the whole list of today's bookings, PHP just printed the word Array and a warning.
"Echo is for showing things to customers," Marcus says. "It's terrible for showing things to you. It hides what type a value is, and it can't show a list at all. When something looks wrong, don't guess — look inside it."
He types one line above the broken part of booking.php: var_dump($isOpen);. The answer appears at once: bool(false).
So when a value looks wrong, I shouldn't guess what's in it — I should ask PHP to show me exactly?
Mia
Printing for Customers vs. Looking Inside for Yourself
echo hides the truth
false and null print as nothing, and "25" looks exactly like 25.
Lists can't be echoed
echo turns a whole list into the single word Array, with a warning.
Debugging needs facts
To fix a bug you need to see the real value and its type, not what you expect it to be.
Debugging with var_dump, print_r and var_export
Debugging: a bug is code that doesn't do what you meant, and debugging is finding out why. The first rule of debugging is simple: look at the real values. Most bugs hide behind a value that isn't what you think it is — a false you thought was true, a "25" you thought was 25, a list that's empty.
echo can't help with that, as the first code box below shows: false and null print as nothing, and a list prints as the word Array plus a warning. A list of values is called an array (you'll learn arrays properly in Act 08). PHP gives you three better tools for looking inside any value:
- var_dump(): shows the type and the value of anything — the most detailed of the three, and the one to reach for first.
- print_r(): shows a value in a short, readable form — nice for lists, but it hides types.
- var_export(): shows a value written as PHP code — the exact code you could paste back into a file.
What echo hides: Mia's three problems in one file. false and null vanish, and the list becomes Array with a warning "Array to string conversion". Nothing here tells you what the values really are.
<?php$isOpen = false;$discount = null;$slots = ["9:00", "9:30", "11:00"]; // an array: a list of valuesecho "Open: ", $isOpen, "\n"; // Open:echo "Discount: ", $discount, "\n"; // Discount:echo "Slots: ", $slots, "\n";// Warning: Array to string conversion// Slots: Array
var_dump(): var_dump() prints each value's type first, then its value: int(25), float(12.5), bool(false), NULL. For text it also shows the length: string(2) "25" means "text, 2 characters long, containing 25". Now the difference between the number 25 and the text "25" is impossible to miss — that was the hidden cause of the price bug in chapter 6. You can pass several values at once, separated by commas.
<?phpvar_dump(25); // int(25)var_dump(12.5); // float(12.5)var_dump("25"); // string(2) "25"var_dump(true); // bool(true)var_dump(false); // bool(false)var_dump(null); // NULL$price = "25";$people = 2;var_dump($price, $people); // string(2) "25" int(2)
var_dump() and lists: for an array, var_dump() shows how many items it has, and then each item with its key (its position or name, in square brackets) and its type and value. The second example is a booking with a customer, a time and whether it's paid — and var_dump() shows clearly that paid is bool(false). (Look closely at string(4) "Zoë": three letters, but length 4. That's because ë takes two bytes — Act 07 explains why that matters.)
<?php$booking = ["customer" => "Zoë", "time" => "9:30", "paid" => false];var_dump($booking);
array(3) {["customer"]=>string(4) "Zoë"["time"]=>string(4) "9:30"["paid"]=>bool(false)}
print_r(): print_r() shows the same booking in a shorter, friendlier layout — easy to read at a glance. But notice what's missing: no types, and the false for paid shows as nothing at all. print_r also prints true as 1 and false and null as nothing, just like echo. So use print_r to look at the shape of a list, and var_dump when you need the exact truth. Add true as a second argument, print_r($value, true), and instead of printing, it returns the text, so you can put it in a message or a log.
<?php$booking = ["customer" => "Zoë", "time" => "9:30", "paid" => false];print_r($booking);// Array// (// [customer] => Zoë// [time] => 9:30// [paid] => <- false shows as nothing// )$text = print_r($booking, true); // returns the text instead of printing it
var_export(): var_export() writes the value out as valid PHP code — with quotes around text and false written as false. It's the least common of the three, but useful when you want to copy a real value straight into a test or a settings file.
<?php$booking = ["customer" => "Zoë", "time" => "9:30", "paid" => false];var_export($booking);// array (// 'customer' => 'Zoë',// 'time' => '9:30',// 'paid' => false,// )
Side by side: the table below puts echo and the three tools next to each other, for the same six values. Read it once, and you'll know which one to reach for: when the question is "what exactly is in here?", the answer is always var_dump().
| The value | echo | print_r() | var_dump() | var_export() |
|---|---|---|---|---|
| 25 | 25 | 25 | int(25) | 25 |
| "25" | 25 | 25 | string(2) "25" | '25' |
| true | 1 | 1 | bool(true) | true |
| false | (nothing) | (nothing) | bool(false) | false |
| null | (nothing) | (nothing) | NULL | NULL |
| an array | Array + warning | readable list | list with types | PHP code |
Only var_dump() and var_export() can tell 25 from "25", or false from null.
In a browser: these tools print plain text with line breaks and spaces for layout. In a browser, HTML squashes that into one line (chapter 5). Wrap the output in <pre> ("preformatted") tags, as below, and the browser keeps the layout.
<?php$slots = ["9:00", "9:30", "11:00"];echo "<pre>";print_r($slots);echo "</pre>";
The everyday debugging loop: when something looks wrong: 1) put a var_dump() just before the line that misbehaves, 2) run the code and read the real type and value, 3) fix the cause, 4) remove the var_dump again. Never leave debugging output in code that goes to the real server: it can show customers private data and internal details. On a server you write to the log instead, with error_log() — it goes to the error log you set up in Act 01, chapter 5. Later, a tool called Xdebug will let you pause code and inspect every value without editing anything (Act 14) — but var_dump() will still be the quickest check, every day of your career.
<?phpdeclare(strict_types=1);$isOpen = false;var_dump($isOpen); // TEMPORARY: bool(false) — so that's why nothing showed// On a server, write to the log instead of the page:error_log('isOpen is ' . var_export($isOpen, true));
Key Takeaway
Don't guess what a value is — look. var_dump() shows the exact type and value of anything; print_r() shows a readable layout of lists but hides types; var_export() writes a value as PHP code. echo is for output, not for debugging. Remove debugging output before code goes to the real server.
Why This Matters
Every developer debugs every day, and the fastest bug hunters are the ones who check facts first. In one week Mia lost time to a false that printed nothing, a "25" that looked like 25, and a list that printed Array. With var_dump(), each of those takes ten seconds to see.
The "remove it afterwards" rule matters just as much: a forgotten var_dump() on Launch Day could show Tom's customers someone else's booking details. Debugging output belongs on your screen or in the log — never on a customer's page.
By Friday, Mia goes through booking.php line by line and can say what every single line does — the tags, every statement, every comment, every name and every value. Marcus grins and hands her a copy of the file with a few things broken on purpose. "Prove it." That's this Act's checkpoint.
