Originally published on zero2php.dev — part of the Zero to PHP course. View this lesson on the original site.

echo vs print

echo outputs text; it is a language construct and can take multiple comma-separated values.

print is also for output; it always returns 1 (so it behaves like an expression).

Example

<?php

declare(strict_types=1);

echo "A\n";
print "B\n";

echo "count=", 3, "\n";

Newlines

Use "\n" inside double-quoted strings (or PHP_EOL if you prefer portability). Learning exercises often expect Unix newlines (\n).

Exercise

Print three lines in order: apple, banana, total=3. Use \n between lines (same effect as the lesson example).

From Zero to PHP — PHP foundations: environment, syntax, and values