PHP: var_dump() function


The var_dump() function is used to display structured information (type and value) about one or more variables.


(PHP 4 and above)


var_dump(variable1, variabl2, ....variablen)


Name Description Required /

The variable being checked Required Mixed*

*Mixed: mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value:


Pictorial presentation of PHP var_dump() function

Pictorial presentation of PHP var_dump() function

Example -1:

echo var_dump($var_name1)."<br>";
echo var_dump($var_name2)."<br>";
echo var_dump($var_name3)."<br>";
echo var_dump($var_name4)."<br>";
echo var_dump($var_name5)."<br>";
echo var_dump($var_name6)."<br>";

Output :

string(4) "a678"
string(3) "678"
string(14) "W3resource.com"

View the example in the browser

Example -2 :

$var_name=array(99,'w3resource',67899.99, array('X','Y',1));

Output :

array(4) {    [0]=>    int(99)    [1]=>    string(10) "w3resource"    [2]=>    float(67899.99)    [3]=>    array(3) {      [0]=>      string(1) "X"      [1]=>      string(1) "Y"      [2]=>      int(1)    }  } 

View the php var_dump() function using array variable in the browser

Redirect the output of var_dump() function in a string

We have already learned that var_dump() function is used to display structured information (type and value) about one or more expressions.The function outputs its result directly to the browser. In the following example the output of the var_dump() function stored in a variable as a string, therefore we can manipulate the output. To execute the example we have used two php functions ob_start() and ob_get_clean(). ob_start() function turns output buffering on where as ob_get_clean() function gets current buffer contents and delete current output buffer.

var_dump("Var_dump output in a string");
$out = ob_get_clean();
echo $out;
echo substr($out,0,20);

Output :

string(27) "Var_dump output in a string" 
string(27) "Var_dump

View the output in the browser

PHP var_dump() vs print_r()

The var_dump() function displays structured information (type and value) about one or more variables.

The print_r() function displays human-readable information about a variable.

See the following two examples :

$fruits = array("Banana", "Apple", "Mango", "Coconut");  

Output :

array(4) { [0]=> string(6) "Banana" [1]=> string(5) "Apple" [2]=> string(5) "Mango" [3]=> string(7) "Coconut" }
$fruits = array("Banana", "Apple", "Mango", "Coconut");  

Output :

Array ( [0] => Banana [1] => Apple [2] => Mango [3] => Coconut )

Practice here online :

See also
PHP Function Reference

Previous: unset
Next: var_export

PHP: Tips of the Day

PHP - how to create a newline character?

Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want:


Single quoted strings, on the other hand, only know the escape sequences \\ and \'.

So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n" or using chr function chr(0x0D).chr(0x0A)), the only other way to have a line break within a single quoted string is to literally type it with your editor:

$s = 'some text before the line break
some text after';

Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n for example).

Ref : https://bit.ly/3hcyege