PHP echo

In PHP, the echo statement is used to output text or variables to the browser or the output stream. It is a language construct rather than a function, so it doesn't require parentheses unless you're passing multiple arguments.

The syntax for using echo is straight forward:

echo expression;

Here, expression can be a string, a variable, or a combination of both. The echo statement outputs the value of the expression to the browser or the output stream.

  1. Outputting a string
  2. This will display "Hello, World!" in the browser or the output stream.

    echo "Hello, World!";
  3. Outputting a variable
  4. This will display "Hello, John" if the variable $name contains the value "John".

    $name = "John";
        echo "Hello, " . $name;
  5. Outputting HTML
  6. This will output an HTML heading tag <h1> with the text "Welcome to my website".

    echo "<h1>Welcome to my website</h1>";
  7. Multiple arguments with parentheses
  8. This will output "Hello World". Note that when using parentheses with echo, you can pass multiple arguments separated by commas.

    echo("Hello", " World");

It's important to note that echo does not return a value, so it cannot be used in expressions or assigned to variables. Its primary purpose is to output content to the browser or the output stream.

Additionally, you can use echo without parentheses in most cases, but using parentheses is required when passing multiple arguments.

Next Article ❯