PHP Displaying Variables: PHP Echo & Print Statements

By Vijay

By Vijay

Image
I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 4, 2025
Edited by Kamila

Edited by Kamila

Image
Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

Explore all about Displaying Variables in PHP with examples. Understand the differences between PHP echo and PHP Print statements to get output text or variables in PHP:

In this tutorial, you will learn how to display variables using PHP echo, PHP print, PHP print_r() & PHP var_dump(), some other uses of echo & print statements, and frequently asked questions (FAQs).

Please note that we have used PHP version 7 in all examples.

Let’s begin!

=> Check All PHP Tutorials Here

Displaying Variables in PHP

Displaying Variables and PHP echo, print, print_r & var_dump

There are two popular methods to get/display output in PHP. They are the echo statement and the print statement.

PHP Echo Statement

Out of the above two methods, echo is the most popular method to get an output. You can use echo with or without parentheses (echo or echo()) as it is not an actual function. The echo does not produce any return value.

You can practice the following examples by running the following programming code.

Example 1: Display text.

<?php

echo "Hello World!";

?>

Output:

Hello World!

Example 2: Display HTML.

<?php

echo "<i>Hello World!</i>";

?>

Output:

Hello World!

Example 3: Display the value of a variable.

<?php

$num = 12;

echo $num;

?>

Output:

12

We can pass multiple arguments separated by commas in echo.

PHP Print Statement

Similar to echo, you can use print with or without parentheses (print or print()) as it is also not an actual function. The return value of print is 1. Furthermore, it only takes one argument.

You can practice the following examples by running the following programming code:

Example 1: Display text

<?php

print "Hello World!";

?>

Output:

Hello World!

Example 2: Display HTML

<?php

print "<i>Hello World!</i>";

?>

Output:

Hello World! 

Example 3: Display the value of a variable.

<?php

$num = 12;

print $num;

?>

Output:

12

Echo vs Print

Both echo and print are similar. However, there are some differences too.

The table below shows the key differences between echo and print statements:

EchoPrint
1No return valueAlways returns 1
2Accepts multiple argumentsAccepts only one argument

Apart from the above key differences, echo is comparatively faster than print.

Example: Passing multiple arguments

In this example, we will pass multiple arguments into the echo and print statements. The echo statement will accept multiple arguments, but the print statement will not accept multiple arguments.

You can practice this example by running the following programming code.

  • Code A – Using the echo statement
<?php

$name = "Alexandra";
$age = 19;

echo $name," is ",$age," years old.";

?>

Output:

Alexandra is 19 years old.
  • Code B – Using the print statement
<?php

$name = "Alexandra";
$age = 19;

print $name," is ",$age," years old.";

?>

The below text shows the browser output of the above programming code (you will get an error message).

Parse error: syntax error, unexpected token "," in C:\xampp\htdocs\test.php on line 6

PHP Print_r

Print_r() is a function that outputs information about a variable in a human-readable way. It is useful when debugging.

Example:

You can practice this example by running the following programming code:

<?php

$num = array(12,24,36);

print_r($num);

?>

Output:

Array ( [0] => 12 [1] => 24 [2] => 36 )

PHP Var_dump

Var_dump() is a function that outputs structured information about a variable. It outputs both the type and the value of a variable. It is very useful when debugging.

Example:

You can practice this example by running the following programming code:

<?php

$num = 12;

var_dump($num);

?>

Output:

int(12)

Print_r vs Var_dump

Let’s try to understand the difference between print_r() and var_dump() with an example.

In this example, we will pass the same argument into print_r() and var_dump() functions.

You can practice this example by running the following programming codes.

  • Code A – Pass an argument into the print_r() function
<?php

$movies = array("Titanic","Jurassic World","The Lion King","The Avengers");

print_r($movies);

?>

Output:

Array ( [0] => Titanic [1] => Jurassic World [2] => The Lion King [3] => The Avengers )

It outputs values as an array.

  • Code B – Pass an argument into the var_dump() function
<?php

$movies = array("Titanic","Jurassic World","The Lion King","The Avengers");

var_dump($movies);

?>

Output:

array(4) { [0]=> string(7) "Titanic" [1]=> string(14) "Jurassic World" [2]=> string(13) "The Lion King" [3]=> string(12) "The Avengers" }

It outputs both values and types as an array.


Frequently Asked Questions

1. What is PHP echo?

Echo is a statement used to display output.

2. Can I echo HTML in PHP?

Yes, you can use echo to display text and HTML code.

3. How do you echo a paragraph?

Please refer to the example below.

<?php
echo “<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur rhoncus ornare felis, interdum eleifend mi aliquet id. Vestibulum egestas semper. </p>”;
?>

4. Is PHP echo a function?

Echo is not an actual function. Therefore, you are not required to use parentheses with it.

5. Can echo accept more than 1 parameter?

Yes, it can accept more than 1 parameter.

6. Is PHP print a function?

Similar to echo, print is also not an actual function, and no need to use parentheses with it.

7. Which statement is faster, echo or print?

Comparatively, echo is faster than print.

8. What are the differences between PHP var_dump and PHP print_r?

The var_dump() function is used to display structured information about variables, including both the value and the type. On the other hand, the print_r() function is used to display information about a variable in a way that’s readable by humans.
Usually, var_dump() is more useful than print_r() when debugging, as it displays both types and values.

Conclusion

There are four popular methods to display the value of a variable: echo, print, print_r, and var_dump. The echo statement and the print statement are more or less the same.

Print_r() outputs information about a variable in a human-readable way, while var_dump() outputs structured information about a variable.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



Leave a Comment