• 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
  • Ruby
  • 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.  Replace Space with Dash in php String
  • 2.  php Convert Degrees to Radians with php deg2rad() Function
  • 3.  In PHP isset Method is Used For Checking if Variable is Defined
  • 4.  How to Check If String Contains Substring in PHP
  • 5.  php array_fill() – Create Array of Length N with Same Value
  • 6.  Replace Underscore with Space in php String
  • 7.  php in_array – Check If Value is in php Array
  • 8.  php Random Number Generator with rand Function
  • 9.  Using strtolower() in php to Convert String to Lowercase
  • 10.  How to Create an Empty Array in php

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy