Home PHP PHP var_dump()

PHP var_dump(): A Beginner's Guide to var_dump Function

Learn how to use var_dump() in PHP to debug code with strings, numbers, arrays, objects, and booleans. var_dump displays information about variables, including their type and value.

By a TechBitBytes Contributor, September 05, 2023
9 MIN READ |

This PHP Arrays tutorial is designed for PHP beginners who want to learn the basics of PHP var_dump() function.

PHP var_dump() is available in PHP 4, PHP 5, PHP 7, PHP 8.

With PHP, you have several ways of viewing a variable; echo, print(), printf(), print_r(), and var_dump(). Each of these has their own strengths and weaknesses and when each can be used. var_dump() is preferred over the others for PHP debugging.

Basically, var_dump() is used to displays structured information about one or more variable including its type and value. In other words, var_dump() dumps information about a variable.

The code block below demonstrates different ways var_dump() is used in PHP programming.

1. var_dump() Strings & Numbers

<?php
	//PHP Code Block 1
	
	//numbers
	$age = 32;
		
	var_dump($age);
	
	//Strings
	$str = "PHP for Beginners on TechBitBytes.com";
		
	var_dump($str);
?>

	C:\wamp64\www\tbb\debugger\index.php:7:int 32
 
	C:\wamp64\www\tbb\debugger\index.php:12:string 'PHP for Beginners on TechBitBytes.com' (length=37)

The first line outputs the var_dump($age) and intelligently displays the data type as int and the value 32. You might have seen the reason why var_dump() is best for debugging. If not, here it is; the function indicates which Line in your PHP code has been executed to display - in our case Line 7.

2. var_dump() Arrays

<?php
	//PHP Code Block 2
	
	$names = array("Mary", "Kevin", "John", "Esther");
		
	var_dump($names);
?>

C:\wamp64\www\tbb\debugger\index.php:6:
array (size=4)
  0 => string 'Mary' (length=4)
  1 => string 'Kevin' (length=5)
  2 => string 'John' (length=4)
  3 => string 'Esther' (length=6)

You do not need to use PHP foreach and for loop to debug arrays. As shown in PHP Code Block 2 above, var_dump() displays the function and goes further to give various properties of the array, which makes it easy to debug PHP code. In our highlighted output, we can see that our array, $names is of size 4 (has for elements). var_dump() loops through the array displaying each element's index value, data type, value, and length of the value (number of characters).

3. var_dump() Objects

var_dump() is the go-to function for PHP programmers when debugging PHP objects. In PHP code block 3, we use var_dump() to dump an object's information.

<?php
	//PHP Code Block 3

	$createCarObj = new CAR("Red",1996);
			
	var_dump($createCarObj);
	
	
	$showCar = $createCarObj->getCarVars();
	
	var_dump($showCar);
	
	class CAR{
		
		public $my_car;
		
		private $_COLOR;
		private $_YOM;
		
		function __construct($color, $yom){
			$this->_COLOR = $color;
			$this->_YOM = $yom;
		}
		
		public function getCarVars(){
			return $this->_COLOR . ': ' . $this->_YOM;
		}
	}
?>

In the code block above, we have two var_dump() functions. In Line 6, the var_dump($createCarObj) statement displays all information about the '$createCarObj' created in Line 4. This object is created from the Car { } class in Line 13.

The output of var_dump($createCarObj) in Line 6 is:


C:\wamp64\www\tbb\debugger\index.php:6:
object(CAR)[1]
  public 'my_car' => null
  private '_COLOR' => string 'Red' (length=3)
  private '_YOM' => int 1996
 

The output of var_dump($showCar) in Line 11 is:


C:\wamp64\www\tbb\debugger\index.php:11:string 'Red: 1996' (length=9)

The above var_dump($showCar) dumps information on the function call public function getCarVars() on Line 25.

 

4. var_dump() Boolean

Boolean data type stores either true or false. With var_dump(), you can get the actual boolean value and not a representation as is the case with print_r(). print_r() returns either "" (false) or 1 (true) for boolean data types.
<?php
	//PHP Code Block 4
	
	//Boolean
	$isSet = true;
		
	var_dump($isSet);
?>

C:\wamp64\www\tbb\debugger\index.php:7:boolean true

 

var_dump() Two or More Variables

Let's revisit our earlier PHP code block 1. We can use less PHP statements by outputting both $age and $str using a single var_dump() as follows.

<?php
	//PHP Code Block 5
	
	//numbers
	$age = 32;
	
	$str = "PHP for Beginners on TechBitBytes.com";
		
	var_dump($age, $str);
?>

C:\wamp64\www\tbb\debugger\index.php:9:int 32
C:\wamp64\www\tbb\debugger\index.php:9:string 'PHP for Beginners on TechBitBytes.com' (length=37)

 

You can learn more about the var_dump() function on PHP.net var_dump

 

  This article is written to the best of the author's knowledge. TechBitBytes(TBB) ensures that all articles are constantly updated with the latest information.