Debugging is as important as coding in the field of development. There might occur a case when the developer needs to check information of a variable such as if a function returns an array it is best to check the return type and the contents of the returned value. A developer may echo all the contents but PHP itself provides a method to do the same and as well as checks the datatype.
The var_dump() function is used to dump information about a variable. This function displays structured information such as type and value of the given variable. Arrays and objects are explored recursively with values indented to show structure. This function is also effective with expressions.
Syntax:
void var_dump ($expsn)
Parameters: The function takes a single argument $expsn that may be one single variable or an expression containing several space separated variables of any type.
Return Type: This function has no return type.
Examples:
Input : $expsn = 2.7; Output : float(2.7) Input : $expsn = array(1, 2, array(3, 4, 5)); Output : array(3) { [0]=> int(1) [1]=> int(2) [2]=> array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } }
Below program illustrates the working of var_dump() in PHP:
<?php // PHP code to illustrate the working // of var_dump() Function var_dump(var_dump(2, 2.1, TRUE, array (1, 2, 3, 4))); ?> |
Output:
int(2) float(2.1) bool(true) array(4) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) } NULL
Important points to note:
- All properties of objects whether public, private or protected will be returned in the output unless the object implements a __debugInfo() method.