HOME HTML EDITOR C JAVA PHP

PHP echo and print Statements

In PHP, there are two basic ways to get output: echo and print. Both are used to display data on the screen, but they have minor differences.

1. The echo Statement

echo is used most frequently because it is slightly faster than print and can take multiple parameters (though this is rarely used).

<?php
  echo "<h2>PHP is Fun!</h2>";
  echo "Hello world!<br>";
  echo "I am learning ", "PHP ", "with multiple params.";
?>

2. The print Statement

print can also be used with or without parentheses. Unlike echo, it always returns a value of 1, so it can be used in expressions.

<?php
  print "<h2>PHP is Fast!</h2>";
  print "Hello world!<br>";
  print "Print only takes one argument.";
?>

3. Outputting Variables

The following example shows how to output text and variables using both statements:

<?php
  $txt1 = "Learn PHP";
  $txt2 = "W3Schools.com";
  $x = 5;
  $y = 4;

  echo "<h2>" . $txt1 . "</h2>";
  echo "Study PHP at " . $txt2 . "<br>";
  echo $x + $y;
?>

Comparison Table

Feature echo print
Speed Marginally faster Slightly slower
Return Value No return value Returns 1
Parameters Can take multiple Can take only one
Tip: Most developers prefer using echo because it is simpler and more efficient for displaying HTML content.