0
0
PHPprogramming~5 mins

Echo statement in PHP

Choose your learning style9 modes available
Introduction

The echo statement is used to show text or values on the screen. It helps you communicate with the user by displaying messages or results.

To display a welcome message on a website.
To show the result of a calculation to the user.
To print variables or text for debugging purposes.
To output HTML content dynamically from PHP code.
Syntax
PHP
echo expression;

The expression can be text in quotes, variables, or a mix of both.

You can use single or double quotes for text, but double quotes allow variables inside the text to be parsed.

Examples
Prints the text Hello, world! on the screen.
PHP
echo "Hello, world!";
Also prints Hello, world! but uses single quotes.
PHP
echo 'Hello, world!';
Prints Hello, Alice! by inserting the variable value inside the text.
PHP
$name = "Alice";
echo "Hello, $name!";
Prints Age: 25 by joining text and a variable with a dot (concatenation).
PHP
$age = 25;
echo "Age: " . $age;
Sample Program

This program sets a name and then prints a welcome message including that name.

PHP
<?php
$name = "Bob";
echo "Welcome, $name!";
?>
OutputSuccess
Important Notes

Echo does not add a new line automatically. Use \n inside double quotes or <br> in HTML to create line breaks.

You can echo multiple items separated by commas, like echo "Hello", " world!";.

Summary

Echo prints text or variables to the screen.

Use quotes for text and variables inside double quotes to show their values.

It is a simple way to communicate with users or show results.