PHP var_dump() function

The var_dump() function is a built-in function of PHP that dumps the information about the variables. This information includes the data type and value of the variable. In case of string, it also includes the size of the string passed inside the function.

The array and object are explored recursively with values to show their structure.

In simple words, this function provides structured information about one or more variables.

Syntax

snippet
var_dump(var1, var2, ...);
Note
Note: It directly outputs the result to the browser.

Parameter

Expression (var1, var2, ...): variable or the value of the variable, which you want to dump.

Return Type

It does not return any value.

Examples

PHP var_dump(): with numeric and Boolean value

In case of numeric and boolean values, it prints only data type and value of the variable. See the example below to understand it better.

snippet
<?php
	//PHP program to demonstrate the working of var_dump function
	$x = 25;
	//dump integer variable
	var_dump ($x);			
	echo "</br>";
	
	$y = 32.5;
	//dump float variable
	var_dump ($y);			
	echo "</br>";
	
	$bvalue = true;
	//dump boolean variable
	var_dump ($bvalue);
?>

Output:

Output
int(25) bool(true)

PHP var_dump(): with string

snippet
<?php
	//dump strings
	$msg1 = "Hello Alex";
	var_dump ($msg1);
	
	echo "</br>";
	$msg2 = "Welcome to rookienerd";
	var_dump ($msg2);
?>

Output:

Output
string(10) "Hello Alex" string(21) "Welcome to rookienerd"

Let's understand with the help of a diagram:

PHP var_dump() function

PHP var_dump(): multiple arguments

The var_dump() function allows us to pass multiple arguments of different types. It can dump two or more variables together.

snippet
<?php
	$x = 23;
	$msg = "Hello world";
	//dump two variables
	var_dump ($x, $msg);	
?>

Output:

Output
int(23) string(11) "Hello world"

PHP var_dump(): with array

The var_dump() function allows arrays to be explored recursively with values to show their structure.

snippet
<?php
	$arr = array ("Mercedes", "BMW", "Audi");
	var_dump ($arr);
	echo "</br> </br>";
	
	$arr1 = array (52, "Bye", 91.3, array ("Apple", "Blackberry", "Android"));
	var_dump ($arr1);
?>

Output:

Output
array(3) { [0]=> string(8) "Mercedes" [1]=> string(3) "BMW" [2]=> string(4) "Audi" } array(4) { [0]=> int(52) [1]=> string(3) "Bye" [2]=> float(91.3) [3]=> array(3) { [0]=> string(5) "Apple" [1]=> string(10) "Blackberry" [2]=> string(7) "Android" } }
Note
Note: The var_dump() function comes under the category of variable handling.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +