php

How to Print Arrays in 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.

//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.

//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

 tag. Create a PHP file named 'prn3.php' and add the following code.

// 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 "
";
//Print output
echo $output;
//Add the ending pre tag of html
echo "
";
?>

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.

//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.

//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 "
";
 
//Print the structure of both arrays
var_dump($product_list1, $product_list2);
 
//Add the ending pre tag of html
echo "
";
 
?>

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.

 
//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 "
";
 
//The output of print_r()
print_r($students);
//The output of var_dump()
var_dump($students);
 
//Add the ending pre tag of html
echo "
";
?>

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

Video Tutorial

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.

Battle For Wesnoth 1.13.6 Development Released
Battle For Wesnoth 1.13.6 released last month, is the sixth development release in the 1.13.x series and it delivers a number of improvements, most no...
League of Legendsin asentaminen Ubuntu 14 een.04
Jos olet League of Legendsin fani, tämä on sinulle mahdollisuus testata League of Legendsia. Huomaa, että PlayOnLinux tukee LOLia, jos olet linux-käyt...
Asenna uusin OpenRA-strategiapeli Ubuntu Linuxiin
OpenRA on ilmainen / ilmainen reaaliaikainen strategiapelimoottori, joka luo uudet Westwood-pelit, kuten klassinen Command & Conquer: Red Alert. Hajau...