PHP Output (echo and print)

echo Statement

echo is the most commonly used output method in PHP. It can output strings, variables, and HTML:

Loading...
Output:

You can echo multiple strings at once (comma-separated):

Loading...
Output:

print Statement

print is similar to echo but:

  • Can only output one argument at a time
  • Returns 1, so it can be used in expressions
Loading...
Output:

Differences Between echo and print

  • echo is faster and more commonly used
  • print returns a value, echo does not
  • echo supports multiple arguments, print does not

Outputting Variables

You can include variables inside double-quoted strings or concatenate:

$language = "PHP";
echo "I love $language!";
echo "I love " . $language . "!";

HTML Output

You can use PHP to output HTML markup:

echo "<ul><li>Apple</li><li>Banana</li></ul>";

Escaping Characters

Use backslashes to escape characters inside strings:

echo "She said: \"PHP is great!\"";

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic