PHP
π PHP var_dump() Function
var_dump() is a powerful built-in debugging function in PHP used to display detailed structured information about one or more expressions. In simple terms, it answers the question: βWhat exact type, length, and value does this variable hold?β
-
Key Purpose: Unlike standard output functions,
var_dump()outputs a variable's data type, value, length (for strings), and element breakdown (for arrays and objects). It is an essential tool for developers during debugging.
π οΈ Basic Usage & Code Examples
1οΈβ£ Scalar Types Inspection
For basic types like integers, floats, strings, booleans, and NULL, var_dump() outputs both the type and value:
$a = 10;
var_dump($a);
// Output: int(10)
var_dump(3.14);
// Output: float(3.14)
var_dump("Merhaba");
// Output: string(7) "Merhaba" (Includes string length)
var_dump(true);
// Output: bool(true)
var_dump(null);
// Output: NULL
2οΈβ£ Inspecting Arrays
When inspecting arrays, var_dump() reveals the element count, array keys, and the exact data type of every nested value:
$dizi = [1, "PHP", true];
var_dump($dizi);
/* Output:
array(3) {
[0]=>
int(1)
[1]=>
string(3) "PHP"
[2]=>
bool(true)
}
*/
3οΈβ£ Inspecting Objects
For object instances, var_dump() prints the class name, object ID, property count, and the property visibility/values:
class Test {
public $a = 5;
}
$obj = new Test();
var_dump($obj);
/* Output:
object(Test)#1 (1) {
["a"]=>
int(5)
}
*/
π Comparison & Best Practices
Understanding how var_dump() compares to other output mechanisms in PHP:
| Feature | var_dump() | print_r() | echo / print |
|---|---|---|---|
| Type Information | β Yes (Exact types) | β No | β No |
| Detailed Structure | β Very Detailed | β οΈ Human-Readable | β Basic String Output |
| String Length Info | β Yes | β No | β No |
| Primary Purpose | Debugging & Analysis | Quick Inspection | User-Facing Output |
π‘ Pro Tip: Formatting Output in Browser
Wrap var_dump() inside HTML <pre> tags when debugging directly in the browser to maintain clear line breaks and indentation:
echo "<pre>";
var_dump($dizi);
echo "</pre>";
-
π Brief Summary
-
var_dump()displays data type + value + internal structure for any variable. - It is intended strictly for development and debugging, never for end-user web pages.
- Crucial for checking incoming form inputs, API payloads, and resolving type mismatches.
