How To Print Array with print_r() In PHP?

PHP provides the array data type which is used to store multiple values or items in a single variable. The array is one of the most popular variable types in PHP which can store lots of different values and iterate easily. Variables can be printed with the echo statement in PHP by providing the variable name. But the echo can not be used to print an array. PHP provides the print_r() function in order to print arrays. In this tutorial, we will examine how to print an array in PHP in different ways.

Create PHP Array

First, we will create an array that will be printed with the print_r() function and echo/for loop in later this tutorial. Even there are different array types in PHP like indexed arrays, associative arrays, multidimensional arrays we will create a simple indexed array. We will use the array() constructor in order to create an array and provide the items as parameter to this constructor.

<?php

$names = array("ismail","ali","ahmet");

echo count($names);

?>

Print PHP Array with print_r() Function

The print_r() is a built-in function provided by PHP too print the arrays items. The syntax of the print_r() function is very simple where two parameters are provided where single parameter is required or must.

print_r($ARRAY,$IS_STORE)
  • $ARRAY is the array that will be printed or displayed. This parameter is mandatory.
  • $IS_STORE is an optional parameter. Normally the print_r() function will output the provided array items to the standard output and do not returns any output about the array. But by providing the $IS_STORE as TRUE the output will be returned by the print_r() which can be set into a variable for later use.

In the following example we will print all items of the given array named $names to the standard output. If this code is executed in web server the output will be put into the HTML output, if executed in the PHP interactive shell it will be displayed on the shell.

<?php

$names = array("ismail","ali","ahmet");

print_r($names);

?>

Put Array Items Into A Variable

The print_r() function also accepts a second parameter called $IS_STORE. By default, the print_r() will display the array content to the standard output. But if we want to return the array items from the print_r() the $IS_STORE should be set as TRUE. In the following example, we will print or put the array items into the variable named $items by setting print_r() second parameter as TRUE.

<?php

$names = array("ismail","ali","ahmet");

$items = print_r($names,TRUE);

echo $items;

?>

Print PHP Array with echo and foreach Loop

Even the print_r() function is the best method to print an array there are alternative ways. The foreach loop and echo commands can be used in order to print an array. But this will not dynamic as the print_r() method. We will create a foreach loop which will iterate over all first-level items of the given array and then we will print them with the echo command.

<?php

$names = array("ismail","ali","ahmet");

foreach ($names as $name) {
    echo $name;
}

?>

But this method is a bit trivial where it should be changed for an array which is multilevel. Multilevel array means an item of an array is an array too.

Leave a Comment