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:

PHP
$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:

PHP
$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:

PHP
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:

PHP
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.