ZetCode

PHP echo Statement

last modified April 16, 2025

The PHP echo keyword is one of the most fundamental language constructs used for outputting data. It can display strings, variables, and HTML content directly to the browser. Unlike functions, echo is a language construct and doesn't require parentheses.

Basic Definitions

The echo statement outputs one or more strings to the browser. It's not actually a function but a language construct. This means you can use it without parentheses.

Echo is slightly faster than print as it doesn't return a value. It can accept multiple parameters when used without parentheses. Echo is commonly used in PHP to generate dynamic HTML content.

Syntax: echo string1, string2, ... or echo(string). The parentheses are optional when passing a single parameter.

Basic echo Usage

This example demonstrates the simplest form of echo to output a string.

basic_echo.php
<?php

declare(strict_types=1);

echo "Hello, World!";

The code outputs the string "Hello, World!" to the browser. This is the most basic usage of echo. No HTML tags are included in this output. The statement ends with a semicolon as all PHP statements do.

Outputting Variables with echo

This example shows how to output variable values using echo.

echo_variables.php
<?php

declare(strict_types=1);

$name = "John";
$age = 30;

echo "Name: $name, Age: $age";

The code demonstrates variable interpolation in double-quoted strings. PHP replaces variables with their values when outputting. This is a common way to combine static text with dynamic values. Single quotes would output the variable names literally.

Multiple Parameters with echo

This example demonstrates passing multiple parameters to echo.

multiple_parameters.php
<?php

declare(strict_types=1);

$firstName = "Sarah";
$lastName = "Connor";

echo "First: ", $firstName, " Last: ", $lastName;

The code shows echo accepting multiple comma-separated parameters. This is more efficient than concatenation for large outputs. Each parameter is output in sequence. This syntax only works without parentheses.

Outputting HTML with echo

This example shows how to output HTML markup using echo.

echo_html.php
<?php

declare(strict_types=1);

$title = "PHP Tutorial";
$content = "Learn PHP programming.";

echo "<div class='article'>";
echo "<h2>$title</h2>";
echo "<p>$content</p>";
echo "</div>";

The code generates complete HTML elements using echo statements. This is a common pattern in PHP templating. Variables are interpolated within the HTML. Each echo outputs a portion of the final page structure.

Using echo with Conditional Logic

This example demonstrates combining echo with if statements.

conditional_echo.php
<?php

declare(strict_types=1);

$loggedIn = true;

if ($loggedIn) {
    echo "Welcome back, user!";
} else {
    echo "Please log in to continue.";
}

The code shows echo being used within conditional blocks. Different messages are output based on the $loggedIn variable. This pattern is common for dynamic content. The echo statements execute based on program logic.

Echo with Concatenation

This example demonstrates string concatenation with echo.

concatenation.php
<?php

declare(strict_types=1);

$product = "Coffee";
$price = 4.99;
$currency = "USD";

echo "Product: " . $product . " Price: " . $price . " " . $currency;

The code uses the concatenation operator (.) to combine strings and variables. This approach is useful when building complex output. Each component is joined into a single string. The final result is then output by echo.

Echo with Heredoc Syntax

This example shows echo using heredoc for multi-line strings.

heredoc.php
<?php

declare(strict_types=1);

$name = "Alice";
$email = "alice@example.com";

echo <<<EOT
<div class="profile">
    <h3>User Profile</h3>
    <p>Name: $name</p>
    <p>Email: $email</p>
</div>
EOT;

The code uses heredoc syntax to output multi-line HTML content. Variables are interpolated within the heredoc block. This maintains the original formatting. The EOT marker must appear on its own line. Heredoc is ideal for large blocks.

Best Practices

Source

PHP echo Documentation

This tutorial covered PHP echo statements with practical examples showing various output techniques in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP basics tutorials.