Chapter 8: Input and Output

A program that never says anything and never asks anything is hard to use. Output is how your program talks to the person running it; input is how it listens back.

Output with echo

echo prints a value and automatically starts the next line. You do not need to add "\n" at the end of an echo statement:

echo "Hello, world!";

$name = "Ada";
echo "Hello, {$name}!";
echo "Two plus two is " . (2 + 2);

Each echo statement ends its output with a line break. This makes it easy to print several values on separate lines without adding anything to each string. Functions such as printf do not add that line break; include \n in their format string only when you want one.

Formatted output: printf and sprintf

When you build a string out of several pieces, a format string is often clearer than a chain of .:

printf("%s = %.2f\n", "price", 9.5);   // price = 9.50
printf("%04d\n", 42);                  // 0042
printf("%s has %d items\n", "Ada", 3); // Ada has 3 items
PlaceholderExpectsExample
%dinteger42
%ffloat9.500000
%.2ffloat, 2 decimals9.50
%sanything, as textAda
%04dinteger, padded to 40042

sprintf does the same thing but returns the string instead of printing it:

$label = sprintf("%04d", 42);
echo "id: " . $label;    // id: 0042

Inspecting values: var_dump and print_r

While learning, you will want to see what a value actually is — not just how it prints, but its type and, for collections, its structure:

var_dump(3.5);                          // float(3.5)
var_dump("hello");                      // string(5) "hello"

print_r(["name" => "Ada", "lang" => "HolyPHP"]);

var_dump is the compact one — type, size, value. print_r lays arrays and maps out as a tree, which is what you want when a nested structure looks wrong.

Input with readline

readline prints a prompt and reads one line from standard input:

$name = readline("Your name? ");
echo "Hello, {$name}!";

$answer = readline("Continue? (yes/no) ");
if ($answer === "yes") {
    echo "carrying on";
}

The value it returns is always a string, and the trailing newline is already removed for you — what comes back is the line itself:

$line = readline("");
echo "you typed " . strlen($line) . " characters: " . $line;

Input that came from a file or another program can still carry stray spaces, so trim() is cheap insurance before you compare:

$answer = trim(readline("Continue? "));
if ($answer === "yes") { echo "carrying on"; }

When the input runs out, readline returns an empty string ("") rather than blocking forever. That makes a program safe to feed from a file or a pipe:

$name = readline("");
if ($name === "") {
    echo "no input";
}
In the playground: there is no terminal attached, so readline returns "" immediately. Everything else on this page — echo, printf, var_dump, print_r — works exactly as it does on your own machine.

Reading a whole file

For input that is not interactive, read it in one call:

$text = file_get_contents("data.txt");
echo strlen($text) . " bytes";
Challenge: Ask for a name and a price, then print a receipt line with printf — pad the price to two decimals — and dump both values with var_dump to see their types.