How to echo an array in PHP

Summary:

Failed to echo your PHP arrays? You can't echo an array directly, but you can use the print_r, var_dump, or var_export functions to echo your array and view its contents. Check out the article on how to implement it.

How to echo an array in PHP

This article explains how to echo an array in PHP and includes a section about many ways we can print out a PHP array. Stick to the article and you will have better insights by the end of this article.

Let’s start without any further ado.

#1 – How to echo an array in PHP with echo

The first method to echo an array is by using the PHP language construct “echo”. It is a popular keyword in PHP that outputs one or more strings. PHP echo expects a string expression and throws an error on an array.

<?php
 
$arr = ["Hello", "World"];
 
echo $arr; //PHP Warning:  Array to string conversion
 
?>

Why “echo” if it doesn’t work with arrays? Well, there is a function in PHP “implode” that joins array elements to form a string. Once the array turns into a string the “echo” prints it out just fine. 

How to echo an array in PHP using the implode function?

PHP implode function joins an array element into a string. 

implode(string $separator, array $array): string

The function joins the elements using the separator argument. The following example uses this function to join the array elements into a string and then echo it.

<?php
 
$arr = ["Hello", "World"];
 
echo implode(" ", $arr); //Hello World
 
?>

Voila! The “echo” prints the string that it receives from the implode function. Here we have an overhead of a function call. But there are better ways. Let’s explore further.

#2 – How to echo an array in PHP with print_r

PHP print_r  prints human-readable information about an array. It doesn’t include any intricate data type information. I always prefer it while working with arrays.

The following example uses print_r.

<?php
 
$arr = ["Hello", "World"];
 
print_r($arr);
 
/*
OUTPUT
Array
(
    [0] => Hello
    [1] => World
)
*/
 
?>

Voila! No overhead of calling an extra function. PHP print_r is a one-liner to output an array.

#3 – How to print an array in PHP with var_dump

PHP var_dump prints an array with details about the data types of all the elements. The output appears a little bit messy especially if you’re dealing with multidimensional arrays.

<?php
 
$arr = ["Hello", "World", 1, true];
 
var_dump($arr);
 
/*
OUTPUT
array(4) {
  [0]=>
  string(5) "Hello"
  [1]=>
  string(5) "World"
  [2]=>
  int(1)
  [3]=>
  bool(true)
}
*/
 
?>

See! It prints out the array with information about the data types of every element.

#3 – How to echo an array in PHP with var_export

PHP var_export is a useful function to echo an array in PHP. It either prints out or returns a parsable string representation of an array. 

var_export(mixed $value, bool $return = false): ?string

If the return argument is true it would return the value rather than printing it. Here’s how.

<?php
 
$arr = ["Hello", "World", 1, true];
 
var_export($arr);
 
/*
array (
  0 => 'Hello',
  1 => 'World',
  2 => 1,
  3 => true,
)
*/
 
?>

Alternatively set the return to true and store the return value to an array. You can then print the variable using any of the above methods.

<?php
 
$arr = ["Hello", "World", 1, true];
 
$arr_output = var_export($arr, true);
 
print_r($arr_output);
 
/*
array (
  0 => 'Hello',
  1 => 'World',
  2 => 1,
  3 => true,
)
*/
 
?>

Voila! We have seen pretty much all the options to print an array in PHP. 

Conclusion

This article includes all the methods to echo an array in PHP. First, it overviews the PHP “echo” construct with the implode function. Next, it goes through the print_r function. Following print_r, the article shows how to echo an array in PHP using var_dump. Finally, it includes a section about var_export.

Hopefully, this article answers your question. Stay tuned at FuelingPHP if you’re interested in similar articles and tutorials about PHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.


Article Categories

Article Tags

© 2022 Confident.Systems