Last Updated: February 25, 2016
·
778
· saji89

var_export is sometimes more useful than var_dump

This is what the PHP documentation says about the two functions:

var_export - Outputs or returns a parsable string representation of a variable

var_dump - Dumps information about a variable

The important thing is that var_dump will give us all the contents of a PHP variable in an understandable manner, but var_export gives us the contents of thae which can directly be used in a PHP file as a variable's value. Other than that when it comes to arrays, var_export values are more readbale at times.

Let's see a real example to understand the difference:

var_dump output for an array:

array(6) {
  ["thework"]=>
  string(3) "112"
  ["fromdate"]=>
  string(10) "25/06/2014"
  ["todate"]=>
  string(10) "30/06/2014"
  ["name"]=>
  string(45) "This is the the name for this entry"
  ["details"]=>
  array(5) {
    [1]=>
    array(2) {
      ["id"]=>
      string(8) "191_25_0"
      ["extraunits"]=>
      string(1) "1"
    }
    [2]=>
    array(2) {
      ["id"]=>
      string(8) "191_26_0"
      ["extraunits"]=>
      string(3) "1.5"
    }
    [3]=>
    array(2) {
      ["id"]=>
      string(8) "191_28_0"
      ["extraunits"]=>
      string(1) "1"
    }
    [4]=>
    array(2) {
      ["id"]=>
      string(8) "192_26_0"
      ["extraunits"]=>
      string(1) "1"
    }
    [5]=>
    array(2) {
      ["id"]=>
      string(8) "192_27_0"
      ["extraunits"]=>
      string(1) "1"
    }
  }
}

var_export output for an array:

array (
  'thework' => '112',
  'fromdate' => '25/06/2014',
  'todate' => '30/06/2014',
  'name' => 'This is the the name for this entry',
  'details' => 
    array (
      1 => 
      array (
        'id' => '191_25_0',
        'extraunits' => '1',
      ),
      2 => 
      array (
        'id' => '191_26_0',
        'extraunits' => '1.5',
      ),
      3 => 
      array (
        'id' => '191_28_0',
        'extraunits' => '1',
      ),
      4 => 
      array (
        'id' => '192_26_0',
        'extraunits' => '1',
      ),
      5 => 
      array (
        'id' => '192_27_0',
        'extraunits' => '1',
      ),
    ),
)

N.B: I have also used echo "<pre>"; to make both the above outputs more readable. Its always helpful to use it for redability.