PHP echo and print Functions

PHP Tutorial

PHP echo and print – Complete Beginner Guide With Examples

Introduction to echo and print in PHP

In PHP, echo and print are language constructs used to display output to the browser. Whenever you want to show text, variables, HTML content, or calculation results, you use echo or print.

They are among the most commonly used statements in PHP programming.

Example:

This displays:

Hello World!

Understanding the difference between echo and print is important for beginners.


What is echo in PHP?

echo is a PHP language construct used to output data to the browser.

Basic Syntax:

Example:

Output:

Welcome to PHP!

What is print in PHP?

print is also a PHP language construct used to output data.

Basic Syntax:

Example:

Output:

Learning PHP is fun!

Key Differences Between echo and print

Although both are used to display output, they are slightly different.

Featureechoprint
Returns ValueNoYes (returns 1)
Can Print Multiple ValuesYesNo
Slightly FasterYesSlightly Slower
Parentheses RequiredNoNo

In most cases, developers prefer echo.


echo Can Output Multiple Values

echo can print multiple strings separated by commas.

Output:

Hello World!

print cannot do this.


Using echo and print with Variables

Example using echo:

Example using print:

Both display variable values correctly.


Combining Text and Variables

You can combine strings and variables using:

 Concatenation Operator (.)


Double Quotes (Variable Interpolation)

 Note:

  • Double quotes allow variable expansion

  • Single quotes do not

Wrong:

Output:

My name is $name

Using HTML Inside echo and print

You can include HTML tags.

This displays a heading.

You can also mix PHP and HTML:


echo and print with Calculations

Output:

15

With print:

Output:

5

print Returns a Value

The main difference is that print returns 1.

Example:

Output:

Hello1

Because print returns 1.

echo does not return anything.


Using echo in Loops

Output:

1
2
3
4
5

Short echo Tag

PHP also provides a shorthand syntax:

This works like:

Short echo is commonly used in templates.


When to Use echo vs print?

Use echo when:

  • Printing multiple values

  • Writing large output

  • Slightly better performance matters

Use print when:

  • You need a return value

  • Using it inside expressions

However, most developers prefer echo.


Common Beginner Mistakes

 Forgetting Semicolon

Must end with:


 Using Single Quotes Incorrectly

Variables don’t work inside single quotes.


 Mixing Concatenation Improperly

Wrong:

Correct:


Performance Difference

echo is slightly faster than print because:

  • It does not return a value

  • It is simpler internally

But in small projects, the difference is negligible.


Real-World Example – Dynamic Web Page

Used in:

  • Login systems

  • Dashboards

  • Dynamic content

  • E-commerce websites


Security Tip When Using echo

When displaying user input:

Use:

This prevents XSS attacks.

Never directly output raw user data.


Summary Table

Featureechoprint
FasterYesSlightly slower
Multiple ArgumentsYesNo
Returns ValueNoYes (1)
Most UsedYesLess common

FAQs About PHP echo and print

1. What is the difference between echo and print?

echo does not return a value and can output multiple strings. print returns 1 and outputs only one string.


2. Which is faster, echo or print?

echo is slightly faster than print.


3. Can echo print variables?

Yes, echo can display variables and expressions.


4. Can print return a value?

Yes, print always returns 1.


5. Which one should beginners use?

Beginners should mostly use echo because it is more flexible and commonly used.


Conclusion

echo and print are essential PHP output statements. They allow you to display text, variables, calculations, and HTML content dynamically.

Key points:

  • echo is faster and more flexible

  • print returns a value

  • Both are easy to use

  • echo is more commonly preferred

Understanding these basics is important before learning:

Mastering output statements is the first step toward building dynamic PHP websites.

You may also like...