PHP echo, print, echo VS print, Variable Interpolation

php echo statement, print statement, echo VS print, Variable Interpolation

Table of Contents –

  • PHP echo Statement
  • PHP print Statement
  • Difference between echo and print
  • PHP 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.

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

You noticed that we are showing an HTML tag in the above example, that means we can display HTML code using this echo statement.

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

And you can also display a variable using this echo statement.

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

Using 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.

// 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 also it can be used without parentheses or with parentheses, like the echo statement. Because it’s not actually a function (it is a language construct).

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
With HTML Tag

PHP echo VS print

Difference between echo and print –

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

print always return 1, but echo doesn’t return any value for that echo is faster than print.

var_dump(print "hello ");
Browser Output
hello int(1)

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

echo "argument1 ","argument2 ","argument3";

echo "<br>";

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

echo "<br>";

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

// you can't do this
// 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.

$name = "John Doe";
echo "Hi, $name";

echo "<br>";// line breaker

// You can do this but it will not work - Read Note
echo 'Hi, $name';
Browser Output
Hi, John Doe
Hi, $name

PHP Variable Interpolation only works with double quotes.

Write Variable without space –

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

$x = "Photo";
echo "Adobe {$x}shop";

//you can't do this
//echo "Adobe $xshop";
Browser Output
Adobe Photoshop

Next is – PHP String, Escape Sequences and Concatenation

Previous is – PHP Variable Declaration, Naming Conventions, Data Types

Posts Navigation

Leave a Reply

Your email address will not be published. Required fields are marked *