• Tutorial List

PHP echo, print, echo VS print, & Variable Interpolation

Last updated May 15, 2020
PHP echo, print, echo VS print, & Variable Interpolation

PHP echo Statement

The PHP echo statement is used to output one or more strings.

How to use the echo statement?

The following example will show you how to display a string and a number using the echo statement.

<?php
  echo "Hi, this is a string"; // Displaying string(text)
  echo "<br>"; //Line breaker
  echo 153; // Displaying number
?>
Browser Output
Hi, this is a string
153

🤔 In the above example, you can see that we are showing an HTML tag, which means that we can display HTML code using this echo statement.

<?php
  // Displaying HTML
  echo "<strong>This text is bold</strong>";
?>
Browser Output
This text is bold

Displaying variables using the echo statement

You can also display a variable using this echo statement –

<?php
  // Displaying variable
  $x = "Hello world";
  $y = 654;
  echo $x;
  echo "<br>";
  echo $y;
?>
Browser Output
Hello world
654

PHP echo statement with parentheses

You can use echo with parentheses. But, echo is not actually a function (it is a language construct), so you are not required to use parentheses with it.

<?php
  // Using parentheses
  echo("Hello world");
?>
Browser Output
Hello world

PHP print Statement

The PHP print statement is also used to output one or more strings, like the echo statement.

And it can also be used without parentheses or with parentheses, like the echo statement. Because it’s not actually a function (it is a language construct).

<?php
  print("with parentheses");

  print "<br>";//line breaker

  print "Without parentheses";

  print "<br>";//line breaker

  $x = "this is a vriable";
  print $x;

  print "<br>";//line breaker

  print 466;

  print "<br>";//line breaker

  print "<strong>With HTML Tag</strong>";
?>
Browser Output
with parentheses
Without parentheses
this is a vriable
466
With HTML Tag

PHP echo VS print

What are the differences between echo and print?

In PHP, there are two major differences between the echo and print statement.

👉 print always returns 1, but echo doesn’t return any value, so that’s why echo is faster than print.

<?php
	var_dump(print "hello ");
?>
Browser Output
hello int(1)

👉 PHP print statement only accepts a single argument but, the echo accepts multiple arguments.

<?php
  echo "argument1 ","argument2 ","argument3";

  echo "<br>";

  echo "Hi ","John ","Doe";

  echo "<br>";

  // with variable
  $x = "Jane Doe";
  echo "Hi ",$x;

  // you can't do that
  // print "Hi, ","John ","Doe";
?>
Browser Output
argument1 argument2 argument3
Hi John Doe
Hi Jane Doe

PHP Variable Interpolation

PHP Variable Interpolation is a way or shortcut to insert variables within a string.

<?php
  $name = "John Doe";
  echo "Hi, $name";

  // you can't do that - Read Note
  //echo 'Hi, $name';
?>
Browser Output
Hi, John Doe

📢 PHP Variable Interpolation only works with double-quotes.


How to write variables without space?

If you want to write a variable without space inside a string, then have to use the curly brackets.

<?php
  $x = "Photo";
  echo "Adobe {$x}Shop";

  //you can't do that
  //echo "Adobe $xShop";
?>
Browser Output
Adobe PhotoShop