Output Statements
Learn all the ways PHP can send output to the browser, from echo and print to print_r, var_dump, and printf.
Introduction
Producing output is one of the most fundamental things a PHP script does — after all, generating a response is the entire point of a web request. PHP offers several different ways to send output, each suited to a different situation: quick display, debugging a variable's exact type and value, or formatting numbers precisely.
- How to use echo, including with multiple comma-separated arguments.
- How print differs from echo, and why print can be used inside an expression.
- How to inspect arrays and objects readably with print_r().
- How to see a value's exact type and content with var_dump().
- How to produce precisely formatted output with printf() and sprintf().
echo
echo is the most common way to output data in PHP. It is technically a language construct rather than a true function, which is why it can accept multiple comma-separated arguments and does not require parentheses.
<?php echo "Hello, World!"; echo "\n";
$name = "Amol"; $age = 25;
// Multiple comma-separated arguments echo "Name: ", $name, ", Age: ", $age;?>Click Run to see what this code prints.
Because echo is a language construct rather than a function, it does not return any value — meaning you cannot use it as part of a larger expression the way you can with print.
print behaves very similarly to echo — it outputs a string — but with two key differences: it only ever accepts a single argument, and it always returns the integer 1, which means it can be used inside expressions.
<?php print "Hello from print!"; print "\n";
// print returns 1, so it can be used in an expression $result = print "This still works.\n"; echo $result; // 1?>Click Run to see what this code prints.
echo vs print
| Feature | echo | |
|---|---|---|
| Return value | None | Always returns 1 |
| Multiple arguments | Yes, comma-separated | No, only one |
| Speed | Marginally faster | Marginally slower |
| Usable in an expression | No | Yes |
In everyday code, the difference rarely matters and comes down to preference — echo is slightly more common because of its ability to take multiple arguments without concatenation.
print_r()
When you try to echo an array directly, PHP does not know how to represent it as a simple string. print_r() solves this by printing arrays and objects in a human-readable, structured format — extremely useful while developing and debugging.
<?php $fruits = ["apple", "banana", "cherry"]; print_r($fruits);
$user = ["name" => "Amol", "age" => 25]; print_r($user);?>Click Run to see what this code prints.
var_dump()
var_dump() goes a step further than print_r() by also showing each value's exact data type and, for strings, their length. This makes it the go-to tool when you need to be certain whether a value is an integer, a float, a string, or a boolean.
<?php var_dump(42); var_dump(3.14); var_dump("hello"); var_dump(true); var_dump(null); var_dump(["a", "b"]);?>Click Run to see what this code prints.
Reach for print_r() when you just want a readable overview of an array or object's contents. Reach for var_dump() when the exact type matters — for example, when debugging why "5" == 5 behaves differently from "5" === 5.
printf() and sprintf()
printf() outputs a formatted string directly, using placeholders (like %s for a string, %d for an integer, and %.2f for a float rounded to two decimals) that get replaced by the arguments you pass afterward. sprintf() works identically but returns the formatted string instead of printing it, so you can store or reuse it.
<?php $name = "Amol"; $price = 19.5;
printf("Hello, %s! Your total is $%.2f\n", $name, $price);
$formatted = sprintf("Order #%04d", 7); echo $formatted;?>Click Run to see what this code prints.
| Placeholder | Meaning |
|---|---|
| %s | String |
| %d | Integer |
| %f | Float (default 6 decimal places) |
| %.2f | Float rounded to 2 decimal places |
| %04d | Integer padded with zeros to 4 digits |
Common Mistakes
- Trying to echo an array directly — it outputs the unhelpful literal text "Array" instead of its contents.
- Using print_r() when you actually need to know a value's exact type — use var_dump() instead.
- Forgetting that printf() prints immediately, while sprintf() only returns a string and prints nothing by itself.
- Mismatching format specifiers, such as using %d for a value that is actually a float.
Best Practices
- Use echo for simple, everyday output.
- Use print_r() (often wrapped in <pre> tags in HTML output) when inspecting arrays or objects during development.
- Use var_dump() when you need to confirm a value's exact type, not just its content.
- Use sprintf() when you need to build a formatted string for later use, such as storing it or passing it to another function.
Frequently Asked Questions
echo is marginally faster since it has no return value to manage, but the difference is negligible in virtually all real applications.
Wrap the call in HTML <pre> tags, like echo "<pre>"; print_r($data); echo "</pre>";, so the browser preserves the whitespace and indentation.
print_r() produces a human-readable summary, while var_export() produces output formatted as valid PHP code that could be pasted back into a script.
Yes — pass true as the second argument, like $text = print_r($data, true);, which returns the formatted output instead of printing it directly.
Key Takeaways
- echo outputs one or more comma-separated values and has no return value.
- print outputs a single value and always returns 1, so it can be used inside an expression.
- print_r() displays arrays and objects in a readable, structured format.
- var_dump() displays each value's exact type alongside its content.
- printf() and sprintf() produce precisely formatted output using placeholders like %s, %d, and %.2f.
Summary
PHP gives you several output tools, each suited for a different job: echo and print for everyday output, print_r() and var_dump() for inspecting data while developing, and printf()/sprintf() for precise, formatted results. Knowing which one to reach for will make both your finished pages and your debugging sessions much smoother.
- You can output values using both echo and print.
- You understand the practical differences between echo and print.
- You can inspect arrays and objects using print_r() and var_dump().
- You can produce formatted output using printf() and sprintf().