Development php

How to Print Arrays in PHP

Array variables are used to store multiple values in a single variable.  Sometimes it is required to check the structure and values of the array variables in human readable format for debugging purposes. You can use two built-in functions of PHP to do the task. These are print_r() and var_dump(). If you want get more detailed information about any array variable then you can use var_dump() because it provides information of array values by including data types. How you can use these functions in PHP is shown in this tutorial using some examples.

Before starting this tutorial, you can read the tutorial on declaring and using array variables in PHP. This will help you to follow this tutorial properly.

Using print_r():

This function displays human readable information of any variable.  The syntax of this function is given below.

mixed print_r (mixed $output[,bool $return = FALSE] )

It has one mixed type mandatory parameter and one Boolean optional parameter. The mandatory parameter contains the output of the function. The default value of the optional parameter is false. If the value of the optional parameter is set to true then the output of the function will return to a variable rather than print to the screen.  This function can be used on different types of variables. In this tutorial, it is used to display the structure of the array variable. Some examples of print_r() with array are given below.

Example – 1:

Create a PHP file named ‘prn1.php’ and add the following code. The optional parameter is not used in this example. So, the output will be printed on the browser.

<?php

//Declare the array
$myarr = array("Name" => "Linuxhint.com", "type" => "tutorial site","content" =>
array("Ubuntu","CentOS","Debian"));

//print the structure of the array
print_r($myarr);

?>

Output:

Open the browser and run the script from the server. The following output will appear after running the script from the server.

http://localhost/phpcode/prn1.php

Example – 2:

Create a PHP file named ‘prn2.php’ and add the following code. The optional parameter is used in this example and set to true. So, the output will be returned to the variable, $output. The variable is printed later.

<?php

//Declare the array
$myarr = array("courseId" => "303", "courseName" => "PHP","duratuon" => "6 Months");
 
//Store return value
$output = print_r($myarr,true);
 
//Print the return value
echo $output;

?>

Output:

The following output will appear after running the script from the server.

http://localhost/phpcode/prn2.php

Example – 3:

You can print the output of this function in more readable way by using html <pre> tag. Create a PHP file named ‘prn3.php’ and add the following code.

<?php

// Declare array variable
$myarr = array("0" => "linuxhint.com", "1" => "is", "2" => "a", "3" => "good",
"4" => "tutorial", "5" => "blog", "6" => "site");

// Store the output of print_r() function
$output = print_r($myarr,true);

//Add the starting pre tag of html
echo "<pre>";

//Print output
echo $output;

//Add the ending pre tag of html
echo "</pre>";

?>

Output:

The following output will appear after running the script from the server.

http://localhost/phpcode/prn3.php

Using var_dump():

var_dump() function is also used to display the structured information of any variable. If you want to know about the data type of each element of any array variable then you can use this function. The syntax of this function is given below.

void var_dump ( mixed $output [, mixed $... ] )

It has one mixed type mandatory parameter and one mixed type optional parameters. This function doesn’t return any value.

Example – 1:

Create a PHP file named dump1.php and add the following PHP code. A simple numeric array is declared in the example and the output prints the array values with data types using var_dump() function.

<?php

//Declare the array
$books = array("Learning HTML 5", "JavaScript basics", "Learning CCS3" ,"
PHP 7 and MySQL 5"
,"JQuery", "Pro AngularJS");

//Print the structure of the array with data type
var_dump($books);

?>

Output:

The following output will appear after running the script from the server.

http://localhost/phpcode/dump1.php

Example – 2:

Create a PHP file named dump2.php and add the following PHP code. Two associative arrays are declared in this example and printed the structure by using var_dump() function.

<?php

//Declare two arrays
$product_list1 = array("Dell Laptop" => 540, "Samsung Monitor" => 70,
"Keyboard" => 15,"Mouse" => 5);

$product_list2 = array("TV" => 660, "Freezer" => 700, "Microwave Oven" => 200,
"Speaker" => 50);
 
//Add the starting pre tag of html
echo "<pre>";
 
//Print the structure of both arrays
var_dump($product_list1, $product_list2);
 
//Add the ending pre tag of html
echo "</pre>";
 
?>

Output:

The following output will appear after running the script from the server.

http://localhost/phpcode/dump2.php

Example – 3:

Create a PHP file named dump3.php and add the following PHP code to find out the difference between print_r() and var_dump() function. In this example, one multidimensional array is declared  and printed by using both print_r() and var_dump() functions.

<?php
 
//Declare a multidimensional array
$students =
array("1109" => array("Name" => "John Paul", "department" =>"BBA", "Batch" => "100th"),
"1274" => array("Name" => "William", "department" =>"EEE", "Batch" => "110th"),
"1703" => array("Name" => "Fahmida Yesmin", "department" =>"CSE", "Batch" => "54th"),  );
 
//Add the starting pre tag of html
echo "<pre>";
 
//The output of print_r()
print_r($students);
//The output of var_dump()
var_dump($students);
 
//Add the ending pre tag of html
echo "</pre>";
?>

Output:

The following output will appear after running the script from the server. The difference of these functions will be cleared if you show the output of both functions for same array variable.

http://localhost/phpcode/dump3.php

CONCLUSION

In any type of programming, debugging is an important part of the development task. The coder can find out the reasons for wrong output of any code by doing proper debugging. Every programming language has some options or functions for debugging purpose. PHP developer can use print_r() and var_dump() functions for debugging when an array variable is not displaying the expected output. I hope this tutorial will help you to know the use of print_r() and var_dump() functions and apply them properly in PHP script for array variables.

About the author

Fahmida Yesmin

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.