Lesson 4 of 50 – Echo and Print in PHP
8%

Echo and Print in PHP

In PHP, echo and print are language constructs used to display output. They are commonly used to display text, variables, HTML, and the results of expressions.

Note: Both echo and print can display output. The main difference is that echo can accept multiple arguments, while print accepts one argument and returns the value 1.
What is echo in PHP?

The echo construct displays one or more strings. It is frequently used to display messages and HTML content.

Example:
<?php
echo "Hello World!";
?>
Output:
Hello World!

The echo construct displays the text written inside the quotation marks.

Displaying Multiple Messages Using echo

You can use multiple echo statements to display different messages.

Example:
<?php
echo "My Name is Rahul";
echo "<br>";
echo "I am learning PHP";
?>
Output:
My Name is Rahul
I am learning PHP

The HTML <br> tag creates a line break in the browser.

Using echo with Variables

You can display the value of a variable using echo.

Example:
<?php
$name = "Rahul";
$age = 20;

echo $name;
echo "<br>";
echo $age;
?>
Output:
Rahul
20

PHP replaces each variable with its current value when executing the statements.

Using echo with HTML

The echo construct can output HTML markup. The browser then interprets that markup and displays the corresponding elements.

Example:
<?php
echo "<h2>Welcome to PHP</h2>";
echo "<p>Learn PHP step by step.</p>";
?>
Output:

Welcome to PHP

Learn PHP step by step.

The browser displays a heading and a paragraph because PHP sends the HTML markup to the browser.

What is print in PHP?

The print construct also displays output. Unlike echo, it accepts one argument and returns the value 1.

Example:
<?php
print "Hello from print!";
?>
Output:
Hello from print!

For ordinary output, echo and print often serve the same purpose.

Using print with Variables

You can also use print to display the values of variables.

Example:
<?php
$name = "Amit";
$course = "PHP Programming";

print $name;
print "<br>";
print $course;
?>
Output:
Amit
PHP Programming

Each print statement displays its argument. The HTML line break separates the output into two lines.

Difference Between echo and print
Feature echo print
Purpose Displays output Displays output
Arguments Can accept multiple arguments Accepts one argument
Return Value No return value Returns 1
Parentheses Usually optional Usually optional
Common Usage Displaying text and HTML Displaying text and HTML
Example: echo with Multiple Arguments
<?php
echo "Hello ", "Students!";
?>
Output:
Hello Students!

The multiple arguments are passed to echo separated by commas. This is not valid syntax for print.

Using Parentheses with echo and print

Parentheses are not required when using echo or print with a single expression.

Example:
<?php
echo "Hello PHP";
print "Welcome Students";

echo("Learning PHP");
print("PHP Tutorial");
?>
Output:
Hello PHPWelcome StudentsLearning PHPPHP Tutorial

All four statements are valid. Notice that no spaces or line breaks appear between the messages unless you output them explicitly.

Key Points
  • echo and print display output in PHP.
  • echo can accept multiple arguments.
  • print accepts one argument.
  • print returns the value 1.
  • Both can display strings and variable values.
  • Both can output HTML markup.
  • Use <br> to create a line break in HTML output.
  • Both are language constructs, not ordinary functions.
Practical Assignment

Complete the following tasks:

  1. Display your name using echo.
  2. Display your institute name using print.
  3. Display your course name and duration.
  4. Use <br> to display messages on separate lines.
  5. Use echo to display an HTML heading and paragraph.
Practice Example:
<?php
echo "<h2>Soopro Pathshala</h2>";
print "Welcome to PHP Tutorials";
echo "<br>";
echo "Keep Learning!";
?>

🧠 Quick Quiz

1. Which PHP construct can accept multiple arguments?




2. What value does print return?




3. Which HTML tag creates a line break?