var_dump()
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:
The PHP syntax of the var_dump()
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:
Example of PHP var_dump()
<?php
$var1 = 10;
$var2 = "Hello, world!";
$array = ["apple", "banana", "cherry"];
var_dump($var1);
var_dump($var2);
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"
}Note:
var_dump()outputs directly to the standard output. In web environments, it automatically wraps the output in<pre>tags for readability. In CLI, it outputs plain text.
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.
Practice
What is the purpose of var_dump() function in PHP?