Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics Unit Converter Integers

Not Understanding the difference between var_dump vs. gettype

Hi all, So I just watched the Intro to PHP video: https://teamtreehouse.com/library/integers

The concept of var_dump is introduced here, but I'm not understanding the purpose of var_dump vs. getttype as they appear to display the same information...although from what I understand you're guessing the variable type with gettype, as you're testing it against a single variable type vs. var_dump that just tells you the variable type.

So if that's the only difference between the two commands, then why do we have gettype as it seems inferior to var_dump?

2 Answers

Matthew Carson
Matthew Carson
5,965 Points

var_dump spits out all of the information inside a variable. While gettype just tells you the 'type' of the variable.

Suppose I had an array of names:

<?php
$people = ['Steve', 'Lynda', 'John', 'Theresa'];
var_dump($people);
/* 
Would Return:
  array(4) {
    [0]=>
    string(5) "Steve"
    [1]=>
    string(5) "Lynda"
    [2]=>
    string(4) "John"
    [3]=>
    string(7) "Theresa"
  }
*/
echo gettype($people);
// Would simply echo out the type of the variable, in this case:
// array

gettype() could be used to for example, if you wanted to perform different operations on values of a different type. Maybe if you wanted to handle a string and an integer differently, you would check for each of the types using gettype().

Echoing (no PHP pun intended) what Matthew wrote, try running this in the console, and in your browser (you'll see the difference):

<?php 
  $num_one = 1;
  var_dump ($num_one);
  echo gettype ($num_one);
 ?>

Sherrie Gossett : I know this is an older reply, but with the var_dump, I'm still not understanding what all the information is that var_dump supplies when run. How do you learn what the output of var_dump means, outside the displaying the type information?