← Back to Chapters

PHP Echo & Print Statement

? PHP Echo & Print Statement

? Quick Overview

In PHP, echo and print are language constructs used to display output in the browser. They are commonly used to print text, variables, and HTML content.

? Key Concepts

  • echo can output multiple values
  • print outputs only one value
  • echo is slightly faster
  • print returns a value

? Syntax & Theory

Both echo and print are not real functions, so parentheses are optional. They are frequently used to generate dynamic content in PHP pages.

? Live Interactive Playground

Type something below to see how echo and print would handle the data.

Using echo

Code: echo $p1, $p2;

Result:

Supports multi-params ✅

Using print

Code: print $p1, $p2;

Result:

Syntax Error ❌

? Code Example: Using echo

? View Code Example
// Using echo to display text and variables
";
$txt = "PHP";
echo "I love ", $txt, " programming.";
?>

? Code Example: Using print

? View Code Example
// Using print to display text and a variable
";
$txt = "Print function";
print "This is a $txt example.";
?>

? Live Output / Explanation

The above examples display simple messages in the browser. echo prints multiple values at once, while print outputs a single value and returns 1.

? Interactive Explanation

Switch between light and dark themes using the toggle button at the top to see how content adapts dynamically.

? Use Cases

  • Displaying user messages
  • Printing variable values
  • Rendering dynamic HTML content
  • Debugging output during development

✅ Tips & Best Practices

  • Prefer echo for general output
  • Use print only when a return value is needed
  • Both can output HTML safely

? Try It Yourself

  • Create a file named echo_print.php
  • Use both echo and print with variables
  • Run it using localhost in your browser