w3resource logo


PHP var_dump function

PHP : var_dump() function

rating Average rating 8 out of 10. Total 64 users rated.

<<PreviousNext>>

Description

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

Version

(PHP 4 and above)

Syntax

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

Parameter

Name Description Required /
Optional
Type
variable1
variable2
---------
variablen

The variable being checked Required Mixed*

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

Return value

Nothing

Example -1 :

<?php
$var_name1=678;
$var_name2="a678";
$var_name3="678";
$var_name4="W3resource.com";
$var_name5=698.99;
$var_name6=+125689.66;          
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 :

int(678)
string(4) "a678"
string(3) "678"
string(14) "W3resource.com"
float(698.99)
float(125689.66)

View the example in the browser

Practice here online :

 

Example -2 :

<?php
$var_name=array(99,'w3resource',67899.99, array('X','Y',1));
var_dump($var_name);
?>
    

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

Practice here online :

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.

<?php
ob_start();
var_dump("Var_dump output in a string");
$out = ob_get_clean();
echo $out;
echo("<br>");
echo substr($out,0,20);
?>
    

Output :

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

View the output in the browser

Practice here online :

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 :

<?php
$fruits = array("Banana", "Apple", "Mango", "Coconut");  
var_dump($fruits); 
?>
    

Output :

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

<?php
$fruits = array("Banana", "Apple", "Mango", "Coconut");  
print_r($fruits); 
?>
    

Output :

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

See also
PHP Function Reference



We have compiled a number of PHP exercises. Check and assess you PHP skill by visiting those exercises.

<<PreviousNext>>