TL;DR: This guide covers five methods to echo or print an array in PHP: using print_r(), var_dump(), var_export(), foreach loop, and implode() function. Each method is explained with code examples and console outputs.
Table of Contents
Using print_r() Function
The print_r() function prints human-readable information about a variable.
$array = ['Monday', 'Tuesday', 'Wednesday'];
print_r($array);
Console Output
Using var_dump() Function
The var_dump() function dumps information about a variable, including its type and value.
$array = ['Monday', 'Tuesday', 'Wednesday'];
var_dump($array);
Console Output
Using var_export() Function
The var_export() function outputs or returns a parsable string representation of a variable.
$array = ['Monday', 'Tuesday', 'Wednesday'];
var_export($array);
Console Output
Using foreach Loop
You can use a foreach loop to iterate through the array and echo each element.
$array = ['Monday', 'Tuesday', 'Wednesday'];
foreach ($array as $key => $value) {
echo "$key => $value\n";
}
Console Output
Using implode() Function
The implode() function joins array elements with a string.
$array = ['Monday', 'Tuesday', 'Wednesday'];
echo implode(', ', $array);
Console Output
Interactive Example
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!