
```html
What are PHP echo and print Statements?
When learning PHP, one of the first things you need to understand is how to display information on a web page. PHP provides two commonly used language constructs for producing output: echo and print. Both can display strings, variables, HTML markup, numbers, and the results of expressions.
Although echo and print perform very similar jobs, there are a few differences between them. Understanding these differences will help you write clearer and more effective PHP programs.
What is Output in PHP?
Output is information that a PHP script sends to its output stream, which in a typical web request becomes part of the response sent to the browser.
For example, if you write:
<?php
echo "Hello, World!";
?>The browser receives output containing:
Hello, World!PHP provides several ways to produce or inspect output, but echo and print are the basic constructs beginners commonly learn first.
What is the PHP echo Statement?
echo is a PHP language construct used to output one or more strings.
A simple example is:
<?php
echo "Hello PHP!";
?>Output:
Hello PHP!The text enclosed inside quotation marks is sent to the output.
Basic Syntax of echo
You can write echo using the following syntax:
echo "Text to display";Because echo is a language construct rather than a regular PHP function, parentheses are not required.
You may sometimes see:
echo("Hello PHP!");However, the simpler form is commonly used:
echo "Hello PHP!";Displaying Text with echo
You can use echo to display any string:
<?php
echo "Welcome to PHP programming.";
?>Output:
Welcome to PHP programming.Displaying Multiple Strings with echo
One useful characteristic of echo is that it can accept multiple comma-separated arguments when it is used without parentheses.
<?php
echo "Learn ", "PHP ", "Step by Step";
?>Output:
Learn PHP Step by StepThis is one difference between echo and print, because print accepts only one argument.
Displaying Variables with echo
PHP variables can also be displayed using echo.
<?php
$name = "Rahul";
echo $name;
?>Output:
RahulYou can combine text and variables:
<?php
$name = "Rahul";
echo "Welcome " . $name;
?>Output:
Welcome RahulThe dot (.) is PHP's string concatenation operator. It joins strings together.
Using Variables Inside Double Quotes
PHP can interpolate many variables inside double-quoted strings.
<?php
$name = "Rahul";
echo "Welcome $name";
?>Output:
Welcome RahulFor improved clarity in more complicated strings, braces can be used:
<?php
$name = "Rahul";
echo "Welcome {$name}";
?>Single Quotes vs. Double Quotes
An important PHP concept is the difference between single-quoted and double-quoted strings.
Consider:
<?php
$name = "Rahul";
echo "Hello $name";
?>Output:
Hello RahulNow consider:
<?php
$name = "Rahul";
echo 'Hello $name';
?>Output:
Hello $nameIn general, variables are interpolated inside double-quoted strings but are treated as ordinary text inside single-quoted strings.
| String Type | Variable Interpolation | Example |
|---|---|---|
| Double Quotes | Yes | echo "Hello $name"; |
| Single Quotes | No | echo 'Hello $name'; |
Displaying Numbers with echo
echo can also output numbers:
<?php
echo 100;
?>Output:
100You can also output the result of a calculation:
<?php
echo 10 + 20;
?>Output:
30Displaying HTML with echo
PHP is commonly used to generate HTML dynamically.
<?php
echo "Welcome to my website.
";
?>The browser interprets the generated markup as HTML.
You can output more complex markup as well:
<?php
echo "Learn PHP Programming";
?>Using echo with HTML Attributes
When generating HTML containing attributes, pay attention to quotation marks.
<?php
echo 'About Us';
?>Here, the PHP string uses single quotes, allowing double quotes to be used conveniently around the HTML attribute.
Combining Multiple Variables with echo
<?php
$firstName = "Rahul";
$lastName = "Sharma";
echo "Full Name: " . $firstName . " " . $lastName;
?>Output:
Full Name: Rahul SharmaDisplaying Array Values with echo
echo cannot directly display an entire array in a useful human-readable form. Instead, access an individual array element:
<?php
$colors = ["Red", "Green", "Blue"];
echo $colors[0];
?>Output:
RedFor debugging arrays, tools such as print_r() and var_dump() are more appropriate.
Example:
<?php
$colors = ["Red", "Green", "Blue"];
print_r($colors);
?>What is the PHP print Statement?
print is another PHP language construct used to output a string.
Example:
<?php
print "Hello PHP!";
?>Output:
Hello PHP!Like echo, print does not require parentheses.
Basic Syntax of print
print "Text to display";You may also see:
print("Hello PHP!");Both forms can be used.
Displaying Variables with print
<?php
$course = "PHP";
print $course;
?>Output:
PHPYou can also combine text and variables:
<?php
$course = "PHP";
print "Course: " . $course;
?>Output:
Course: PHPDisplaying HTML with print
print can output HTML just like echo:
<?php
print "Welcome to PHP programming.
";
?>Difference Between echo and print
echo and print are very similar, but they are not completely identical.
| Feature | echo | |
|---|---|---|
| Purpose | Outputs one or more strings. | Outputs one string. |
| Language Construct | Yes | Yes |
| Parentheses Required | No | No |
| Multiple Arguments | Supported when used without parentheses. | Not supported. |
| Return Value | No return value. | Returns 1. |
| Common Usage | Very common. | Less commonly used. |
Important Difference: print Returns a Value
One notable difference is that print returns the integer value 1, whereas echo does not have a return value.
Because print produces a value, it can be used in some expressions where echo cannot.
For example:
<?php
$result = print "Hello PHP";
?>The text is displayed and $result receives the value 1.
You cannot use echo in the same way:
$result = echo "Hello PHP";This is invalid PHP syntax.
Multiple Arguments with echo
echo supports multiple arguments when written without parentheses:
<?php
echo "PHP", " is ", "easy to learn.";
?>Output:
PHP is easy to learn.The equivalent syntax is not valid with print:
print "PHP", " is ", "easy to learn.";print accepts only a single argument.
Using echo Inside HTML
A common PHP development pattern is to place PHP expressions inside an HTML document.
<!DOCTYPE html>
<html>
<body>
<?php
$name = "Rahul";
?>
Welcome, <?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8'); ?>
</body>
</html>This technique is useful when a page contains mostly HTML with small pieces of dynamic PHP-generated content.
PHP Short Echo Syntax
PHP provides a convenient short syntax specifically for echo:
<?= $name ?>This is equivalent to:
<?php echo $name; ?>For example:
Welcome, <?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>
The short echo syntax is especially useful in templates where PHP variables need to be displayed inside HTML.
Displaying Special Characters
Escape sequences can be used inside double-quoted strings.
<?php
echo "PHP\nProgramming";
?>When running PHP from the command line, \n creates a newline.
Common escape sequences include:
| Sequence | Meaning |
|---|---|
\n | New line. |
\r | Carriage return. |
\t | Horizontal tab. |
\" | Double quotation mark inside a double-quoted string. |
\\ | Backslash. |
Remember that a newline in generated HTML source does not necessarily create a visible line break in the browser. HTML elements such as paragraphs or determine visual layout.
Using echo with Calculations
You can output the result of mathematical expressions directly:
<?php
echo 50 + 25;
?>Output:
75Or store the result in a variable:
<?php
$price = 500;
$quantity = 3;
$total = $price * $quantity;
echo "Total: ₹" . $total;
?>Output:
Total: ₹1500Using echo with Conditional Statements
echo is frequently used inside conditions:
<?php
$age = 20;
if ($age >= 18) {
echo "You are eligible.";
} else {
echo "You are not eligible.";
}
?>Output:
You are eligible.Using echo with Loops
echo is also commonly used to output values generated by loops.
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . "
";
}
?>The browser displays:
1
2
3
4
5Using echo with Arrays and foreach
<?php
$courses = ["PHP", "HTML", "CSS", "JavaScript"];
foreach ($courses as $course) {
echo htmlspecialchars($course, ENT_QUOTES, 'UTF-8') . "
";
}
?>This outputs each array element on a separate line in the browser.
Outputting User Data Safely
A very important concept for beginners is that you should not blindly echo user-controlled data into an HTML page.
For example, avoid outputting untrusted input directly:
<?php
echo $_GET['name'];
?>If data is being inserted into normal HTML text, encode it appropriately:
<?php
$name = $_GET['name'] ?? '';
echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
?>htmlspecialchars() converts characters that have special meaning in HTML into HTML entities. This is an important technique for preventing untrusted text from being interpreted as HTML markup in that output context.
Always remember that output escaping depends on where the value is being inserted. HTML text, HTML attributes, JavaScript, CSS, and URLs have different output-handling requirements.
echo vs. print_r() vs. var_dump()
Beginners sometimes confuse echo and print with debugging functions such as print_r() and var_dump().
| Tool | Main Purpose |
|---|---|
echo | Output strings and scalar values. |
print | Output a string and return 1. |
print_r() | Display human-readable information about variables, especially arrays. |
var_dump() | Display detailed debugging information including data types and values. |
For example:
<?php
$user = [
"name" => "Rahul",
"age" => 25
];
print_r($user);
?>For more detailed debugging:
<?php
var_dump($user);
?>These debugging functions are generally more useful than echo when inspecting arrays and complex values.
Common Mistake: Forgetting the Semicolon
PHP statements normally end with a semicolon.
Correct:
<?php
echo "Hello";
?>Incorrect:
<?php
echo "Hello"
echo "World";
?>The missing semicolon causes a syntax error.
Common Mistake: Incorrect Quotation Marks
This code is invalid:
echo "Welcome to "PHP" programming";You can escape the internal quotation marks:
echo "Welcome to \"PHP\" programming";Or use single quotes around the PHP string:
echo 'Welcome to "PHP" programming';Common Mistake: Trying to echo an Array
This is not an appropriate way to inspect an entire array:
<?php
$colors = ["Red", "Green", "Blue"];
echo $colors;
?>Instead, use:
print_r($colors);or:
var_dump($colors);If you only need one value, access the array element directly:
echo $colors[0];echo and print Quick Comparison
| Task | Example |
|---|---|
| Display text with echo | echo "Hello"; |
| Display text with print | print "Hello"; |
| Display a variable | echo $name; |
| Join strings | echo "Hello " . $name; |
| Output multiple echo arguments | echo "Hello ", $name; |
| Short echo syntax | <?= $name ?> |
| Inspect an array | print_r($array); |
| Inspect type and value | var_dump($variable); |
Which Should You Use: echo or print?
For normal PHP output, echo is generally the more common choice. It is simple, familiar to PHP developers, has no return value, and can output multiple arguments when used with the appropriate syntax.
Use print when its return value is specifically useful or when you prefer its syntax. In everyday PHP development, there is usually little practical reason to replace echo with print.
Best Practices
<?= ... ?> syntax when displaying simple values inside templates.Quick Practice Exercise
Create a PHP file named output.php and add:
<?php
$name = "Rahul";
$course = "PHP";
$price = 499;
echo "Student Name: " . $name;
echo "
";
echo "Course: " . $course;
echo "
";
echo "Course Price: ₹" . $price;
echo "
";
print "Welcome to PHP Programming!";
?>The browser displays:
Student Name: Rahul
Course: PHP
Course Price: ₹499
Welcome to PHP Programming!Try changing the variable values and observe how the generated output changes.
Quick Checklist
<?= ... ?> provides short echo syntax.Conclusion
echo and print are fundamental PHP language constructs used to produce output. They can display text, variables, numbers, HTML markup, and expression results, making them essential tools when creating dynamic web pages.
The main differences are straightforward: echo can output multiple arguments when used without parentheses and has no return value, while print accepts a single argument and returns the integer value 1. In most everyday PHP development, echo is the more commonly used option.
Learning echo and print also introduces several important PHP concepts, including strings, variables, concatenation, quotation marks, HTML generation, loops, conditions, and safe output handling. Once you are comfortable with these concepts, you will be better prepared to build dynamic PHP pages and move on to variables, data types, operators, forms, databases, and complete web applications.