Home » Programming » PHP » Php Var Dump

PHP var_dump() Function [With Examples]

The PHP programming language includes various built-in variable types and allows you to create your own complex object classes.

Third-party packages also come with their own object classes. So it’s useful to find out what type of variable a variable is, its value, and the type/value of any properties or values the variable may contain.

The PHP var_dump() function returns a given variable or value’s properties, including the value and type. It works through the variable’s properties or value recursively doing the same. Read on to learn the syntax and see examples of var_dump usage.

var_dump Syntax

?var_dump ( mixed $value , mixed ...$values ) : void

Note that:

  • $value should be the value or variable you wish to explore the details of
  • $values can be any number of additional values or variables to explore the details of
  • var_dump() does not return any data – it displays the information about the given variable in an indented format
    • This will be output to the console or the document where var_dump is called from

var_dump Examples

Here are a few examples of the kind of output you might expect when running var_dump() on various variable types:

Using var_dump() on a String

$var = "Hello LinuxScrew";
var_dump($var);

Outputs;

string(16) "Hello LinuxScrew"

Using var_dump() on a Number

$var = 3;
var_dump($var);

The above will output:

int(3) 

Below, you’ll see the difference between how variables are typed in PHP – the above number was interpreted and given the integer type. In contrast, in the below example, the float type has been automatically given as it is a decimal number.

$var = 5.1;
var_dump($var);

The above will output:

float(5.1) 

Using var_dump() on an Array

var_dump will work recursively through arrays and objects displaying information for everything contained in them:

$var = [1, 2, ["foo", "bar"]];
var_dump($var);

The above will output:

array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    array(2) {
        [0]=>
        string(3) "foo"
        [1]=>
        string(3) "bar"
    }
}

Using var_dump() on an Object

var_dump() also works on custom classes and objects. Below, a custom class is defined with a string and integer attribute, a variable is created with that class, and the var_dump() is run on it:

class screwClass {
    public $name = "default";
    public $num = 7;
}

$var = new screwClass();
var_dump($var);

The above will output:

object(screwClass)#1 (2) {
    ["name"]=>
    string(7) "default"
    ["num"]=>
    int(7)
}

Conclusion

var_dump() is super helpful when debugging your PHP code.

If you’re just getting started learning PHP, subscribe to us on Twitter for regular refreshers on common PHP functions!

 

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment