• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / php var_dump() Function – Print Information about Variable Type and Value

php var_dump() Function – Print Information about Variable Type and Value

March 31, 2022 Leave a Comment

The php var_dump() function allows us to print information about the type and value of a variable.

$num = 5;
$string = "This is a string; 
$array = ["This","is","an","array"];

var_dump($num, $string, $array);

//Output:
int(5)
string(16) "This is a string"
array(4) {
  [0]=>
  string(4) "This"
  [1]=>
  string(2) "is"
  [2]=>
  string(2) "an"
  [3]=>
  string(5) "array"
}

When working with variables, the ability to easily be able to print out all information about a variable is very useful.

The php var_dump() function returns a data structure which includes information about the passed variables type and value.

With var_dump(), you can see the type and values of strings, ints, floats, arrays and objects.

Below is an example of using var_dump() to output different types of variables.

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}

$num = 5;
$string = "This is a string";
$array = ["This","is","an","array"];

var_dump(new exampleClass);
var_dump($num);
var_dump($string);
var_dump($array);

//Output:
object(exampleClass)#1 (2) {
  ["id"]=>
  NULL
  ["name":"exampleClass":private]=>
  NULL
}
int(5)
string(16) "This is a string"
array(4) {
  [0]=>
  string(4) "This"
  [1]=>
  string(2) "is"
  [2]=>
  string(2) "an"
  [3]=>
  string(5) "array"
}

How to Print Arrays in php with var_dump() Function

We can print arrays in php is with the php var_dump() function.

As we know, var_dump() returns information about a variable that includes its type and value.

So, if you have an array, you can pass it to var_dump() and get a data structure which has the type and value of each of the elements of the array.

Below is an example in php showing how to print an array with var_dump().

$array = ["This","is","an","array"];

var_dump($array);

//Output:
array(4) {
  [0]=>
  string(4) "This"
  [1]=>
  string(2) "is"
  [2]=>
  string(2) "an"
  [3]=>
  string(5) "array"
}

Using var_dump() to Print Objects in php

If you have an object, you can pass it to var_dump() and get a detailed listing the public and private variables in the object.

Below is an example in php showing how to print an object with var_dump().

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}

var_dump(new exampleClass);

//Output:
object(exampleClass)#1 (2) {
  ["id"]=>
  NULL
  ["name":"exampleClass":private]=>
  NULL
}

Hopefully this article has been useful for you to learn how to use the php var_dump() function to dump variable information.

Other Articles You'll Also Like:

  • 1.  php session_destroy() – Delete Session File Where Session Data is Stored
  • 2.  Get Days in Month Using php
  • 3.  php Move File from One Directory to Another Folder
  • 4.  Replace Underscore with Space in php String
  • 5.  php max – Find Maximum Value of Array in php
  • 6.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 7.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 8.  preg_replace php – Use Regex to Search and Replace
  • 9.  php cos – Find Cosine of Number in Radians Using php cos() Function
  • 10.  php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy