PHP

📢 What Is PHP echo?

In PHP, the echo construct is used to output text, variables, or dynamic content directly to the web browser screen. It is one of the most fundamental and frequently used language constructs in PHP development.

  • In Short: echo = Display / Output content to the client screen.

1️⃣ Basic Usage & Printing Strings

Text strings must be enclosed inside single or double quotation marks.

CSS

<?php
echo "Hello World";
?>

  • Browser Output: Hello World

You can print any custom string statement directly:

CSS

<?php
echo "I am learning PHP";
?>

2️⃣ Printing Variables & Concatenation

Variables stored in memory can be rendered to the screen directly or concatenated with text strings using the dot (.) operator:

Printing a Variable:

CSS

<?php
$name = "Michael";
echo $name;
?>

Text + Variable Together:

CSS

<?php
$name = "Alex";
echo "Hello " . $name;
?>

  • Browser Output: Hello Muhammet
  • Note: The dot (.) symbol serves as PHP's string concatenation operator.

3️⃣ Outputting HTML Markup with echo

Since browsers parse standard HTML, you can include HTML elements inside echo statements to dynamically generate DOM elements:

CSS

<?php
echo "<h1>Title</h1>";
echo "<p>This is a paragraph</p>";
?>

4️⃣ Advanced echo Techniques

Printing Multiple Values (Comma Separated):
Unlike print, the echo construct accepts multiple comma-separated parameters:

CSS

<?php
echo "PHP", " ", "is", " ", "easy";
?>

Short Echo Syntax (<?= ?>):
Useful for clean inline template rendering:

CSS

<?= "Hello PHP" ?>
				

  • Note: Short echo syntax is equivalent to <?php echo ... ?>. Standard syntax is still recommended for maximum server compatibility.

Line Breaks with HTML:
To create newline breaks on web pages, use HTML <br> tags inside your output strings:

CSS

<?php
echo "First line<br>";
echo "Second line";
?>

  • 📝 Brief Summary

  • echo outputs text, HTML elements, and data directly to the web browser.
  • Strings are written inside double or single quotes; variables can be joined using the dot (.) operator.
  • It is one of the essential building blocks for generating dynamic web pages in PHP.