Thursday, August 7, 2014

Difference between print_r and var_dump

The var_dump function displays structured information about variables/expressions including its type andvalue. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.
The print_r() 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. Similar notation is used for objects.



print_r(null) will return nothing where as var_dump(null) returns NULL which is useful when debugging
Example:

<?php

//$obj = (object) array('qualitypoint', 'technologies', 'India');

$obj =array('qualitypoint', 'technologies', 'India');
//var_dump($obj) will display below output in the screen.



echo "<br>var_dump out put<BR>";
var_dump($obj);
//var_dump(0);
var_dump($obj[0]);

/*
object(stdClass)#1 (3) {
 [0]=> string(12) "qualitypoint"
 [1]=> string(12) "technologies"
 [2]=> string(5) "India"
} */
//And, print_r($obj) will display below output in the screen.
echo "<br>Print_r out put<BR>";
print_r($obj);
print_r($obj[0]);
/*stdClass Object (
 [0] => qualitypoint
 [1] => technologies
 [2] => India
) */

?>

1 comment: