print_r() Versus var_dump()

There are many functions available in PHP to debug a variable. This function can be used to store value in an array or other another object and it is important to troubleshoot the codes.

Let us see some of them such as var_dump(), print_r() and var_export().

Using the var_dump function you can display the structured information about the variables and expression along with its type and value. Arrays can be explored recursively with intended values to show the structure.

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.

$obj = (object) array(‘qualitypoint’, ‘technologies’, ‘India’);

var_dump($obj) will display below output in the screen.

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.

stdClass Object ( [0] => qualitypoint [1] => technologies [2] => India )

And it seems var_dump($someobject); shows only public properties , but
print_r($someobject) shows all properties (explicitly identifying protected/private properties)

Posted in Php Tagged with: , ,