The echo Function

The echo function outputs the passed strings. Unlike the print function, echo can take multiple parameters and does not return a value. It is one of the most frequently used constructs in PHP for outputting data.

Syntax

phpecho string1, string2, ..., stringN;

Example

Simple string output:

php<?php
  echo 'Hello World';
?>

Code execution result:

phpHello World

Example

Outputting multiple strings:

php<?php
  echo 'Hello', ' ', 'World';
?>

Code execution result:

phpHello World

Example

Outputting variables:

php<?php
  $name = 'John';
  $age = 25;
  echo 'Name: ', $name, ', Age: ', $age;
?>

Code execution result:

phpName: John, Age: 25

Example

Usage with HTML tags:

php<?php
  echo '<b>text</b>';
?>

See Also

  • the printf function,
    which formats and outputs a string