Printing an Array in PHP

To print or echo an array in PHP, you can use the print_r($variable, $return) or var_dump($variable1, $variable2, ...) functions. The print_r() function prints information about the passed variable in human-readable form. The first parameter is the "variable" we want to get information about. The second optional parameter determines whether the function should print or return the result. The default value is "False". If the parameter value is "True", the function will return the value as if it were printed to the output stream. The var_dump() function takes one or more arguments and displays structured information about the variables, including the type and value. In this Print PHP Array example, we use the print_r() function to print information about a given array. Below you can see more examples of PHP array printouts with a detailed description of each method. Click Execute to run the PHP Print Array Example online and see the result.
Printing an Array in PHP Execute
<?php
$colors = array("black", "white", "grey");

print_r($colors);
?>
Updated: Viewed: 9928 times

What is PHP?

PHP is a general-purpose server-side programming language used for web development that can be embedded in HTML code. PHP combines web development technologies like HTML, CSS, JavaScript, and SQL. With its robust developer community and open-source nature, PHP has an extensive collection of libraries and frameworks that significantly improve the efficiency and speed of web application development. PHP is also cross-platform, which means it can run on various operating systems such as Linux, Windows, and macOS.

What is an array in PHP?

An array in PHP is a variable used to store data containing various elements. The elements of an array are identified by a unique key/index, which can be a number or a string. PHP has two types of arrays: indexed arrays and associative arrays. PHP provides many built-in functions for working with arrays, including sorting, searching, splitting a string to array, getting the length of an array, converting an array to JSON, and converting an array to string.

How to print an array in PHP?

To print an array in PHP, you can use the built-in print_r() function, which prints or displays information about a variable, including its type and value.

PHP print_r() Syntax
print_r(variable, return)

Where:
  • variable: specifies the array about which we want to get information
  • return (Optional): when set to "True", this function will return information, but not print it. The default value is "False".
PHP print_r() Example
<?php
$colors = array("black", "white", "grey");

print_r($colors);
?>

#output: Array
#(
#    [0] => black
#    [1] => white
#    [2] => grey
#)

PHP Print Array Examples

The following are examples of printing an array in PHP:

Displaying information about an array using  var_dump()

To display information about an array in PHP, you can use the var_dump() function, which dumps structured information such as the type and value of a given variable. Arrays and objects are explored recursively with indented values to show structure.

PHP var_dump() Syntax
var_dump(variable1, variable2, ...);

Where:
  • variable: defines an array to display information
PHP var_dump() Example
<?php
$colors = array("black", "white", "grey");

echo var_dump($colors);
?>

#output: array(3) {
#  [0]=>
#  string(5) "black"
#  [1]=>
#  string(5) "white"
#  [2]=>
#  string(4) "grey"
#}

Printing the elements of an array using a foreach loop

In some cases, you may want to display additional (for example, dynamically calculated) information about the elements of an array in PHP. In such a case, you can use a foreach loop, which provides a simple iteration of the elements of an array.

PHP foreach() Example
<?php
$colors = array("black", "white", "grey");

foreach ($colors as $value) {
  echo "$value ";
}
?>

#output: black white grey

Printing the elements of an array using the implode()

The following is an example of printing an array using the implode() function. The implode() function concatenates array elements into a string.

PHP implode() Example
<?php
$colors = array("black", "white", "grey");

echo implode(", ", $colors);
?>

#output: black, white, grey

See also