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