Printing expressions is often done in PHP using echo. However, to print an array in PHP, we look for other options because echo directly doesn’t work with arrays and throws array to string conversion error instead.
In this article, we will explore options to print an array in PHP. We’ll also see a way to print an array with echo as well. So, let’s jump straight to the topic.
Option 1 – Print an array in PHP using a foreach loop
Arrays are iterable and could be looped over. There are multiple ways to loop over an array in PHP, as one of our articles explains in detail. However, the point here is to print an array, and we’ll do so using a foreach loop.
With the foreach loop, we have an edge to customize the output the way we like. We have a customized output that says, “My name is $name”. The $name comes from the array as we loop through it. See it yourself in the code to get the point.
<?php
//An array containing names.
$names_arr = array("John","Stuart","Sophie","Sara","Adam");
//foreach to loop over the array.
foreach($names_arr as $name)
{
//Prints each name in the array.
echo "My name is ".$name."\n";
}
/**
* OUTPUT
* My name is John
* My name is Stuart
* My name is Sophie
* My name is Sara
* My name is Adam
*/
?>
Try out the code at your end and customize the message the way you prefer. A good developer always plays with the code and sort things out.
At this point, you might be wondering if there’s a more convenient way to print an array in PHP? Luckily, yes, and that’s the print_r method which we see next.
Option 2 – Print an array in PHP using the print_r function
PHP has the print_r function that could print practically any valid expression in PHP in a readable form. The print_r function works pretty well with arrays. I prefer it every time I want to see what the array looks like. First, let’s see the function arguments as defined in the PHP’s documentation.
print_r(mixed $value, bool $return = false)
Arguments
- $value – The expression to be printed.
- $return – Stores the output to a variable if it is set to true. The default value is false.
Let’s print an array in PHP using the print_r function.
<?php
//An array containing names.
$names_arr = array("John","Stuart","Sophie","Sara","Adam");
//Prints an array to the console.
print_r($names_arr);
/**
* OUTPUT
* Array
* (
* [0] => John
* [1] => Stuart
* [2] => Sophie
* [3] => Sara
* [4] => Adam
* )
*/
?>
See, we can print an array in a one-liner with print_r. We can also set the $return argument to true to return the output rather than printing it. We can pass the return value to a variable and print that, but that would be redundant in our case.
There’s another function to print an array in PHP, which is var_dump. Let’s see what’s the difference between these two functions.
Option 3 – Print an array in PHP using the var_dump function
PHP’s var_dump function goes an extra mile and includes data type information along with values. We can often use it if we want to know the array length and elements’ data types. PHP documentation describes the function in depth.
var_dump(mixed $value, mixed ...$values)
Arguments
- $value – Expression to be printed.
- $values – Further expression to print.
The code below uses var_dump to print an array.
<?php
//An array containing names.
$names_arr = array("John","Stuart","Sophie","Sara","Adam");
//Prints an array to the console.
var_dump($names_arr);
/*
OUTPUT:
array(5) {
[0]=>
string(4) "John"
[1]=>
string(6) "Stuart"
[2]=>
string(6) "Sophie"
[3]=>
string(4) "Sara"
[4]=>
string(4) "Adam"
}
*/
?>
Great. One last thing about var_dump is that we can print more than one array in a single call.
<?php
//An array containing names.
$names_arr = array("John","Stuart","Sophie","Sara","Adam");
$more_names = array("Kevin","Sam","Kylie");
//Prints an array to the console.
var_dump($names_arr,$more_names);
/*
OUTPUT
array(5) {
[0]=>
string(4) "John"
[1]=>
string(6) "Stuart"
[2]=>
string(6) "Sophie"
[3]=>
string(4) "Sara"
[4]=>
string(4) "Adam"
}
array(3) {
[0]=>
string(5) "Kevin"
[1]=>
string(3) "Sam"
[2]=>
string(5) "Kylie"
}
*/
?>
Option 4 – Print an array in PHP using echo with implode
There’s a neat trick to print an array with echo, which otherwise throws array to string conversion error. The idea is to convert the array to string and then print it out to console with echo. But first, let’s understand implode function.
implode(string $separator, array $array)
Arguments
- $separator – String that separates elements of an array. The default is an empty string.
- $array – Array to be converted to a string.
Let’s see it in code.
<?php
//An array containing names.
$names_arr = array("John","Stuart","Sophie","Sara","Adam");
//Implode converts array to string with , separator and echo prints it to console.
echo implode(',',$names_arr);
/*
OUTPUT:
John,Stuart,Sophie,Sara,Adam
*/
?>
Superb. We print out the array with a comma-separator. That’s pretty much it. We hope you’ve enjoyed the article.
Want to explore further about PHP arrays?
We have many fun articles related to PHP arrays. You can explore these to learn more about arrays in PHP.