var_dump in PHP is one of the most popular server-side scripting languages for developing online applications. Its ease of use makes it simple to track problems critical to guarantee a quick code-test-learn feedback loop, no matter what you're building. 

Because var_dump in PHP was created before modern browsers, it didn't have an easy way to log errors to the browser console, even though the browser is the most common way to interact with websites and web applications. PHP is specifically designed for web application development. Also logging to the console with JavaScript is as easy as this.

In the world of development, debugging is just as vital as coding. When a developer has to verify information about a variable, for example, if a function returns an array, it's best to double-check the return type and the contents of the returned value. A developer can echo the entire contents, but var_dump in PHP has a function that also checks the datatype.

To dump information about a variable, use the var_dump in PHP. This function provides structured data about the specified variable, including its type and value. Recursively, arrays and objects are examined, with values indented to demonstrate structure. This function works well with expressions as well.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

Syntax:

void var_dump ($expsn)

$expsn is a single-argument function that accepts a single variable or an expression containing several space separated variables of any type.

Return Type: There is no return type for this function.

Example 1

<?php

// PHP code to illustrate the working

//  of var_dump() Function 

var_dump(var_dump(2, 2.1, TRUE, array(1, 2, 3, 4)));

?>

Output

Var_dump_1

Example 2

<?php  

$a = array(1, 2, array("a", "b", "c"));  

var_dump($a);  

?>  

<?php  

$var1=678;  

$var2="a678";  

$var3="678";  

$var4="Simplilearn.com";  

$var5=698.99;  

$var6=+125689.66;  

echo var_dump($var1)."<br>";  

echo var_dump($var2)."<br>";  

echo var_dump($var3)."<br>";  

echo var_dump($var4)."<br>";  

echo var_dump($var5)."<br>";  

echo var_dump($var6)."<br>";  

?> 

Output

Var_dump_2

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

The Dump Helper Function

When you var_dump in PHP the information about the variable, it's a little laborious to always echo the opening pre> and ending /pre> tags.

You may make things easier by creating a function that you can reuse. For instance, consider a function to be a reusable piece of code that may be referred to by a name. It's possible for a function to have both input and output.

Many built-in PHP functions, such as var_dump in PHP, are available. You can also create your own functions with it. User-defined functions are what they're called. In the function lesson, you'll learn more about it.

The following defines a variable-accepting function called var_dump in PHP. It dumps the variable's information and wraps the output with the pre> tag.

Example:

<?php

$message = 'Dump and die example';

echo '<pre>';

var_dump($message);

echo '</pre>';

die();

echo 'After calling the die function';

Output

Var_dump_3

Use of var_dump() Function

  • First, use the var dump() function to dump information about the $message variable.
  • Second, invoke the die() function to quickly end the script.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion 

Let us look at what we learned about var_dump in brief.

  • To dump information about a variable, use the var dump() function.
  • To make the output of the var_dump() method more legible, wrap it with a pre tag.
  • The die() function immediately exits the script.
  • To dump and die, use the var dump() and death() functions together.

This was all about the small and easy var_dump in PHP that will help you do multiple tasks on PHP. If you want to learn the concept to the core, then enroll in the Simplilearn’s Full Stack Web Development course and advance your career. 

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.