w3docs logo

Introduction

The var_dump() function is a built-in function in PHP that displays structured information about a variable or an expression, including its type and value. It can be used for debugging purposes to inspect the contents of a variable.

Syntax

The syntax of the var_dump() function is as follows:

void var_dump(mixed $expression[, mixed $... ])

The function takes one or more parameters. Each parameter represents the variable or expression to be dumped.

Example Usage

Here is an example of how to use the var_dump() function in PHP:

<?php
$var1 = 10;
$var2 = "Hello, world!";
$array = ["apple", "banana", "cherry"];
var_dump($var1) . "\n";
var_dump($var2) . "\n";
var_dump($array);
?>

In this example, we define three variables: $var1 is an integer, $var2 is a string, and $array is an array. We use the var_dump() function to display structured information about each variable. The output shows the type and value of each variable:

int(10)
string(13) "Hello, world!"
array(3) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "banana"
  [2]=>
  string(6) "cherry"
}

Conclusion

The var_dump() function is a useful tool for displaying structured information about a variable or an expression in PHP. It can be used for debugging purposes to inspect the contents of a variable, including its type and value. By using this function, developers can gain insight into the structure of their data and diagnose issues that may arise in their code.


Do you find this helpful?