php

PHP var_dump() Function

The var_dump() function is a built-in function of PHP to find out the information about the value and data type of one or more variables. It also provides information about the length of the data for the string variable. The information about both scalar and compound variables can be retrieved by using this function. Different uses of this function have been shown in this tutorial.

Syntax

The function can take one or more arguments of different types of variables. The first argument of this function is mandatory and other arguments are optional. It returns nothing.

void var_dump(mixed $var, mixed ...$var);

Uses of var_dump() Function

The use of the var_dump() function to get the information of different types of variables has shown in this part of the tutorial.

Example-1: Use of var_dump() to Dump the Number and Boolean Variables

Create a PHP file with the following script to get the dump information of the integer, float, and boolean variables using the var_dump() function. The boolean, integer, and float variables have been declared in the script. The dump values of these variables will be printed in the output by calling the var_dump() function three times. The dump values of multiple variables can be printed by using the single var_dump() function that has shown in the third example.

<?php

   //Assign boolean value

   $boolVar = True;

   //Print the output of var_dump() for boolean variable

   echo "<b>The output for boolean variable: </b>";

   var_dump ($boolVar);

   echo "</br>";


   //Assign integer value

   $intVar = 756;

   //Print the output of var_dump() for integer variable

   echo "<b>The output for integer variable:</b>";

   var_dump ($intVar);

   echo "</br>";

 
   //Assign floating value

   $floatVar = 45.34;

   //Print the output of var_dump() for floating variable

   echo "<b>The output for floating variable: </b>";

   var_dump ($floatVar);

   echo "</br>";

?>

Output:

The following output will appear after executing the above script. The dump value of each variable contains the data type with the value.

Example-2: Use of var_dump() to Dump String Variable

Create a PHP file with the following script to get the information of the string variable using var_dump() function. A string variable of multiple words has declared in the script. The dump values of this string variable will be printed in the output.

<?php  

    //Assign string value

  $stringVar = "Welcome to LinuxHint";

  //Print the output of var_dump() for string variable

  echo "<b>The output for the string variable: </b>";

  var_dump ($stringVar);

?>

Output:

The following output will appear after executing the above script. The dump value of the string variable contains the length of the string with the data type and the value. According to the output, the length of the string, “Welcome to LinuxHint” is 20.

Example-3: Use of var_dump() to Dump Multiple Variables

Create a PHP file with the following script to get the dump information of multiple variables using single var_dump() function. The string, integer and float variables have declared in the script. The dump values of these variables will be printed in the output.

<?php

   //Assign string variable

   $strVal = "This is a string value ";

   //Assign an integer variable

   $intVal = 50;

   //Assign a float variable

   $floatVal = 4.78;

   //Print dump values of three variables

   echo "<b>The var_dump() output of three variables:</b><br/>";

   var_dump ($strVal, $intVal, $floatVal);

?>

Output:

The following output will appear after executing the above script. The output shows the combined dump values of the string, integer, and float variables together with a space.

Example-4: Use of var_dump() to Dump Array Variables

Create a PHP file with the following script to get the information of the array variables using the var_dump() function. A numeric array of string values has been declared at the beginning of the script that contains 6 elements. The var_dump() function has been used to dump the values of this array. Next, an associative array of 4 elements has been declared in the script. The var_dump() function has been used to dump the values of this associative array also.

<?php

   //Declare an numberic array of strings

    $fruits = array ("Mango", "Banana", "Grape", "Watermelon", "Jackfruit", "Guava");

   //Print the dump values of the array

   echo "<b>The dump value of the numeric array:</b><br/>";

   var_dump ($fruits);

   echo "</br></br>";


   //Declare an associative array of strings

   $foods = array("Cake" =>"$20", "Donut" =>"$5", "cookie" =>"$15", "dark chocolate" =>"$5");

   //Print the dump values of the array

   echo "<b>The dump value of the associative array:</b><br/>";

    var_dump ($foods);

?>

Output:

The following output will appear after executing the above script. According to the output, the index values of both arrays have been printed using the third brackets. The values of the arrays have printed the data type and value for all types of data, and the length of the string data.

Example-5: Print the formatted var_dump() Output Using <pre>Tag

Create a PHP file with the following script to know the way of getting the formatted output of the var_dump() function. A string and integer variables have been declared in the script. The original dump output and the formatted dump output of these variables will be printed. Here, the <pre> tag has been used to print the formatted dump output.

<?php


//Initialize a string variable

$strVal = " Learn PHP Programming  ";

$intVal = 50;


//Print the dump value of var_dump()

echo "<b>The output of var_dump(): </b><br/>";

var_dump ($strVal, $intVal);

echo '<br/>';


//Print the formatted dump value of var_dump() using <pre>tag

echo "<br/><b>The formatted output of var_dump(): </b><br/>";

echo "<pre>";

var_dump ($strVal, $intVal);

echo '<br/>';

echo "</pre>";


?>

Output:

The following output will appear after executing the above script. The output shows that the difference between the original and formatted output of the dump value.

Conclusion

The dump information of the variable is required for debugging purposes mainly. The way to get the dump information of integer, float, boolean, string, and array variables has been explained in this tutorial that will help the PHP users to know the uses of this function properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.