Difference between echo, print, and print_r in PHP

Difference between echo, print, and print_r in PHP

Many times in the project we have to display the record or need to check the variable value while testing in the project. We use many inbuilt PHP helpers to print data in PHP.

The beauty or easiness of PHP is it gives us more options to do the same thing but with little difference. Sometimes we use them normally but sometimes we need to be very specific while using them in the application. So in this article, we are going to discuss the Difference between echo, print, and print_r in PHP.

In this article, we are going to discuss echo, print, and print_r in PHP. You will get to know the basic difference in them which will help you to make your decision to use them wisely in the project.

Use of Echo

  • echo is used to display the parameters that are passed to it.
  • It shows the outputs of one or more variables separated by commas.
  • echo is not a function and returns no value or returns void.
  • echo cannot be used as a variable function in PHP.
  • We cannot echo array.

Lets check the following example.

<?php 
  
// PHP program to illustrate echo 
$x = "Atcodex "; 
$y = "tutorial portal"; 
  
// Display the value of $x 
echo $x, $y; 
?> 

Output:

Atcodex tutorial portal

Use of print

  • print is not a real function and always returns the value 1, so we can use it as an expression.
  • The difference between echo and print is, print only accepts only one argument at a time.
  • print cannot be used as a variable and it outputs only the strings.
  • We cannot print array.

Lets check the following example.

<?php 

// PHP program to illustrate echo 
$x = "atcodex"; 
// Display the value of $x 
print $x; 
?> 

Output:

atcodex

Use of print_r():

  • print_r() is a regular function and returns the complete information rather than just print.
  • print_r() outputs the detailed information about the input.
  • The print_r() function prints the information about a variable in a more human-readable way.
  • We can print_r() array and the string value.

Lets check the following example.

<?php 

// PHP program to illustrate echo 

$arr = array('0' => "atcodex", 
			'1' => "mouse", 
			'2' => "table", 
			'3' => "Gym"); 

// Display the value of $x 
print_r($arr); 
?> 

Output:

Array
(
    [0] => atcodex
    [1] => mouse
    [2] => table
    [3] => Gym
)

Above we have discussed in detail about echo, print, and print_r().

Let’s check echo, print, and print_r() in one code snippet and understand it.

<?php 

$a = "atcodex"; 
$b = array('0' => "codex", '1' => "for", '2' => "codex"); 
$c = 3.14; 
$d = 7; 

// Single argument 
print "\n$a\n"; 

// Multiple argument 
echo $c + $d . "\n"; 

// Return with internal output buffering 
print_r($b); 
?> 

Output:

atcodex
10.14
Array
(
    [0] => codex
    [1] => for
    [2] => codex
)
 

Conclusion:

In the above article we got to know, in how many ways we can print the data in the PHP. It is very easy to use echo. print and print_r in the PHP.

Hope it helped. If you have any queries and doubt, please do comment below and let us know.

You can also check PHP Sessions