What is PHP var_dump() Function?
If you want to display detail information (like data type, length, value) about one or more variables, use var_dump() function. This function is helpful for debugging code by understanding the structure and the content of the variable. It is particularly useful for arrays and objects.
Syntax:
var_dump(variable1, variable2, …)
Parameters:
The Function has 1 required parameter and any number of optional parameters-
variable1 (Required): It specifies a variable.
variable2,… (Optional): It specifies more variables.
Return Values:
The function doesn’t return any value.
Examples:
Example 1:
<pre>
<?php
$languages = array("Markup" => "HTML", "Styling" => "CSS", "Scripting" => "Javascript", "Serverside" => "PHP");
var_dump($languages);
?>
</pre>
Output:
array(4) {
["Markup"]=>
string(4) "HTML"
["Styling"]=>
string(3) "CSS"
["Scripting"]=>
string(10) "Javascript"
["Serverside"]=>
string(3) "PHP"
}
Example 2:
<pre>
<?php
$hi = "Hi,there. ";
$greetings = "Welcome to PHP";
var_dump($hi, $greetings);
?>
</pre>
Output:
string(10) "Hi,there. "
string(14) "Welcome to PHP"
Example 3:
<pre>
<?php
class Car {
public $brand = "Tesla";
private $model = "Model Y";
public function transport(){}
}
$car_obj = new Car();
var_dump($car_obj);
?>
</pre>
Output:
object(Car)#1 (2) {
["brand"]=>
string(5) "Tesla"
["model":"Car":private]=>
string(7) "Model Y"
}
Notes on var_dump() Function:
Though the function displays detail of variables, the output is hard to read on the screen as it doesn’t automatically format the breaks or indentation with HTML. That’s why, we can use <pre> tags to add breaks and spacing.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP var_dump() Function
var_dump() is a built-in variable handling function. It is an extremely useful function in your development stage to debug variables.