PHP Echo/Print


In PHP, displaying output on the browser is essential for creating interactive web pages. PHP provides two primary commands for this purpose: echo and print. They allow developers to send text, variables, or even HTML content to the user’s browser. Understanding how and when to use them is crucial for beginners and is one of the first skills you’ll need to build dynamic web pages.

What Is Echo in PHP?

echo is a language construct used to display one or more strings. It is the most commonly used method for output because it is fast, flexible, and can handle multiple arguments.

<?php
// Display a simple message
echo "Hello, World!"; 

// Display multiple strings using commas
echo "This is ", "a PHP ", "echo example.";
?>
  • Multiple strings can be displayed separated by commas

  • echo does not return any value

  • Ideal for printing dynamic content such as variables or HTML

Tip: echo is slightly faster than print, making it preferable for large scripts.

What Is Print in PHP?

print is another way to display output. Unlike echo, it returns a value of 1, which allows it to be used in expressions.

<?php
// Display a message using print
print "Hello, PHP Print!";

// Using print in an expression
$result = print "Hello again!"; // $result will be 1
echo "<br>Result of print: " . $result;
?>
  • Can only take one argument at a time

  • Slightly slower than echo due to the return value

  • Useful when you want to combine output with logic

Differences Between Echo and Print

Feature Echo Print
Arguments Can take multiple arguments Can take only one argument
Return Value None Returns 1
Speed Slightly faster Slightly slower
Usage Most commonly used Less common

Both work perfectly for basic output, and the choice depends on personal preference or specific use cases.

Using Echo and Print with Variables

Variables can be printed directly with echo or print, which is especially useful when you want to display user input or dynamically generated content.

<?php
$name = "Vicky";
$age = 25;

// Using echo
echo "My name is $name and I am $age years old.";

// Using print
print "Hello $name, you are $age years old.";
?>
  • Double quotes allow variable interpolation

  • Single quotes display the variable name literally, not its value

<?php
echo 'My name is $name'; // Output: My name is $name
?>

Using Concatenation with Echo and Print

You can combine strings and variables using the concatenation operator (.):

<?php
$firstName = "Vicky";
$lastName = "Sanjana";

// Concatenate strings using dot operator
echo "Full Name: " . $firstName . " " . $lastName;

// Using print with concatenation
print "Full Name: " . $firstName . " " . $lastName;
?>
  • Concatenation works with both echo and print

  • Essential for building dynamic content like messages, HTML, or combined strings

Printing HTML Using Echo / Print

You can print HTML tags directly with PHP:

<?php
echo "<h1>Welcome to PHP Echo Example</h1>";
print "<p>This paragraph is printed using print function.</p>";
?>
  • Output is sent as HTML to the browser

  • PHP does not execute HTML, it simply outputs it

You can also combine variables with HTML:

<?php
$username = "Vicky";
echo "<h2>Hello, $username!</h2>";
print "<p>Welcome to the PHP tutorial website.</p>";
?>
  • This demonstrates dynamic HTML generation using PHP variables

Using Escape Sequences

When printing strings, sometimes you need special characters like quotes or new lines. PHP supports escape sequences:

<?php
// Using double quotes inside double quotes
echo "She said, \"Hello, World!\"<br>";

// New line in browser using <br>
echo "Line 1<br>Line 2";

// Tab space using \t (works in CLI, not in browser)
echo "Column1\tColumn2";
?>
  • \" allows double quotes inside double-quoted strings

  • <br> is needed for line breaks in the browser

  • \t works when printing to the command line

Advanced Example: Combining Variables, HTML, and Echo

<?php
$product = "Laptop";
$price = 499.99;
$availability = true;

echo "<h1>Product Details</h1>";
echo "<p>Product: " . $product . "</p>";
echo "<p>Price: $" . $price . "</p>";
echo "<p>Status: " . ($availability ? "Available" : "Out of stock") . "</p>";
?>
  • This example combines variables, conditional output, and HTML

  • Demonstrates how echo can handle complex expressions

Common Mistakes with Echo / Print

  1. Forgetting quotes around strings:

// Incorrect
echo Hello World; // Error

// Correct
echo "Hello World";
  1. Forgetting semicolon at the end:

echo "Hello"; // Semicolon is required
  1. Mixing up single and double quotes:

$name = "Vicky";
echo 'Hello $name'; // Outputs literal $name

Best Practices

  • Use echo for multiple arguments and faster execution

  • Use print when you need a return value

  • Prefer double quotes for variables inside strings

  • Concatenate carefully for dynamic messages or HTML

  • Keep output readable and consistent, especially when mixing PHP and HTML

Summary of the Tutorial

  • echo and print are the primary ways to display output in PHP.

  • echo can take multiple arguments and does not return a value.

  • print can only take one argument but returns 1.

  • Both can display strings, variables, HTML, and dynamic content.

  • Escape sequences like \" and <br> allow formatting output correctly.

Mastering echo and print is essential for any PHP developer, as almost every script requires sending output to the browser. Understanding their differences, usage, and best practices ensures cleaner and faster code.


Practice Questions

  1. Write a PHP script that prints “Hello, PHP!” using echo and include a comment explaining what the script does.

  2. Create a PHP file that defines a variable $name = "Vicky" and prints “Hello, Vicky!” using echo with double quotes. Add a comment explaining variable interpolation.

  3. Write a PHP script that prints two separate messages in a single echo statement using commas. Include comments explaining each string.

  4. Create a PHP file that uses print to display “Welcome to PHP Print Example” and assigns the return value to a variable. Add a comment explaining what the return value represents.

  5. Write a PHP script that prints HTML headings and paragraphs using echo and print. Include comments explaining how HTML is output by PHP.

  6. Create a PHP script that concatenates two variables $firstName = "Vicky" and $lastName = "Sanjana" and prints the full name. Add comments explaining the dot operator.

  7. Write a PHP script that uses escape sequences to include quotes inside a string and a line break using <br>. Include comments explaining each escape sequence.

  8. Create a PHP file with a boolean variable $isAvailable = true and use echo to print “Available” if true and “Out of stock” if false. Include comments explaining the conditional output.

  9. Write a PHP page that prints a product name and price using both echo and print. Include comments explaining the difference in their usage.

  10. Create a PHP file that demonstrates single quotes versus double quotes by printing a variable inside each. Include comments explaining the difference in output.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top