Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 6 of 9
Output Functions & String Formatting
PHP has multiple output mechanisms - each with different use cases. echo and print output strings, printf formats output like C's printf, and var_dump/print_r are for debugging. Understanding sprintf for building formatted strings is especially important for generating SQL LIKE queries, template strings, and log messages.
echo, print, printf, sprintf & Debugging
<?php
declare(strict_types=1);
// ── echo - most common, not a function ──────────────────────────
echo "Hello World
";
echo "Hello", " ", "World
"; // can take multiple arguments
// ── print - returns 1, can be used in expressions ───────────────
$result = print("Hello
"); // prints, $result = 1
$x = true and print("yes"); // valid but unusual
// ── printf - formatted output (like C) ──────────────────────────
printf("Author: %-20s | Posts: %4d | Views: %,.0f
", "Alice", 12, 45000);
// Author: Alice | Posts: 12 | Views: 45,000
// Format specifiers:
// %s = string %-20s = left-aligned, 20 wide
// %d = integer %04d = zero-padded, 4 wide
// %f = float %.2f = 2 decimal places
// %05.2f = 5 total width, 2 decimal, zero-padded
// ── sprintf - same as printf but RETURNS the string ─────────────
// Use sprintf when you need the string for further processing
$logLine = sprintf(
"[%s] [%s] User %d: %s",
date("Y-m-d H:i:s"),
"INFO",
42,
"Login successful"
);
// [2026-03-20 08:34:01] [INFO] User 42: Login successful
$price = sprintf("$%.2f", 49.9); // "$49.90"
$slug = sprintf("/posts/%d/%s", 5, "hello-world"); // "/posts/5/hello-world"
// ── number_format - for currency and thousands ───────────────────
$revenue = 1234567.891;
echo number_format($revenue, 2, ".", ","); // 1,234,567.89
// ── Debugging output ─────────────────────────────────────────────
$data = ["id" => 1, "tags" => ["php", "web"], "active" => true];
var_dump($data); // shows types + values - development use only
print_r($data); // human-readable, no types
var_export($data); // PHP array syntax output - useful for config files
// In HTML context - wrap in pre for readability
echo "<pre>" . print_r($data, true) . "</pre>";
// ── ob_start - output buffering ───────────────────────────────────
ob_start();
echo "This goes to buffer";
$output = ob_get_clean(); // capture and delete buffer
echo strtoupper($output); // THIS GOES TO BUFFERQuick Quiz
Tip
Tip
Practice Output Functions String Formatting in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Output Functions & String Formatting
Practice Task
Note
Practice Task - (1) Write a working example of Output Functions String Formatting from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Output Functions String Formatting is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?