Difference between print_r() and var_dump()

print_r() and var_dump() both are used for the array.

print_r()

It displays information about a variable in a way that’s readable by humans. array values will be presented in a format that shows keys and elements.
example:-


<?php 
$employees=array("Sachin","Rohit","Virat","M.S Dhoni");
print_r($employees);
?>
Output:- Array ( [0] => Sachin [1] => Rohit [2] => Virat [3] => M.S Dhoni )

Try it Yourself

var_dump()

It displays structured information about variables/expressions including its type and value.
example:-


<?php 
$employees=array("Sachin","Rohit","Virat","M.S Dhoni");
var_dump($employees);
?>
Output:-
array(4) { [0]=> string(6) “Sachin” [1]=> string(5) “Rohit” [2]=> string(5) “Virat” [3]=> string(9) “M.S Dhoni” }

Try it Yourself

Difference between print_r() and var_dump() – Objective Questions (MCQs)

Q1. Which function provides more detailed type and value information?






Q2. Which function is better for debugging arrays and objects?






Q3. Which function returns output as string if second parameter is true?






Q4. print_r() output is ______.






Q5. Which can show the length and data type of values?