InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Dumping Variable Contents as Strings in PHP
PHP

Dumping Variable Contents as Strings in PHP

InfinityCoder November 21, 2016

You want to inspect the values stored in a variable. It may be a complicated nested array or object, so you can’t just print it out or loop through it.

Use var_dump(), print_r(), or var_export(), depending on exactly what you need.
The var_dump() and print_r() functions provide different human-readable representations of variables.
The print_r() function is a little more concise:

1
2
$info = array('name' => 'frank', 12.6, array(3, 4));
print_r($info);

prints:

1
2
3
4
5
6
7
8
9
10
11
Array
(
    [name] => frank
    [0] => 12.6
    [1] => Array
        (
               [0] => 3
               [1] => 4
        )
 
)

While this:

1
2
$info = array('name' => 'frank', 12.6, array(3, 4));
var_dump($info);

prints:

1
2
3
4
5
6
7
8
9
10
11
12
13
array(3) {
  ["name"]=>
  string(5) "frank"
  [0]=>
  float(12.6)
  [1]=>
  array(2) {
    [0]=>
    int(3)
    [1]=>
    int(4)
  }
}

The var_export() function produces valid PHP code that, when executed, defines the exported variable:

1
2
$info = array('name' => 'frank', 12.6, array(3, 4));
var_export($info);

prints:

1
2
3
4
5
6
7
8
9
array (
  'name' => 'frank',
  0 => 12.6,
  1 =>
  array (
    0 => 3,
    1 => 4,
  ),
)

The three functions mentioned in the Solution differ in how they handle recursion in references.

Because these functions recursively work their way through variables, if you have references within a variable pointing back to the variable itself, you would end up with an infinite loop unless these functions bailed out.
When var_dump() or print_r() has seen a variable once, it prints *RECURSION* instead of printing information about the variable again and continues iterating through the rest of the information it has to print.

The var_export() function does a similar thing, but it prints null instead of *RECURSION* to ensure its output is executable PHP code.

Consider the arrays $user_1 and $user_2, which reference each other through their friend elements:

1
2
3
4
5
6
7
8
9
10
11
12
13
$user_1 = array('name' => 'Max Bialystock',
                'username' => 'max');
 
$user_2 = array('name' => 'Leo Bloom',
                'username' => 'leo');
 
// Max and Leo are friends
$user_2['friend'] = &$user_1;
$user_1['friend'] = &$user_2;
 
// Max and Leo have jobs
$user_1['job'] = 'Swindler';
$user_2['job'] = 'Accountant';

The output of print_r($user_2) is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Array
(
    [name] => Leo Bloom
    [username] => leo
    [friend] => Array
        (
           [name] => Max Bialystock
           [username] => max
           [friend] => Array
               (
                  [name] => Leo Bloom
                  [username] => leo
                  [friend] => Array
*RECURSION*
                 [job] => Accountant
               )
 
           [job] => Swindler
        )
 
   [job] => Accountant
)

When print_r() sees the reference to $user_1 the second time, it prints *RECUR SION* instead of descending into the array. It then continues on its way, printing the remaining elements of $user_1 and $user_2.

The var_dump() function behaves similarly:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
array(4) {
  ["name"]=>
  string(9) "Leo Bloom"
  ["username"]=>
  string(3) "leo"
  ["friend"]=>
  &array(4) {
     ["name"]=>
     string(14) "Max Bialystock"
     ["username"]=>
     string(3) "max"
     ["friend"]=>
     &array(4) {
        ["name"]=>
        string(9) "Leo Bloom"
        ["username"]=>
        string(3) "leo"
        ["friend"]=>
        *RECURSION*
        ["job"]=>
        string(10) "Accountant"
     }
     ["job"]=>
     string(8) "Swindler"
  }
  ["job"]=>
  string(10) "Accountant"
}

As does var_export(), but with null instead of *RECURSION*:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
array (
  'name' => 'Leo Bloom',
  'username' => 'leo',
  'friend' =>
  array (
    'name' => 'Max Bialystock',
    'username' => 'max',
    'friend' =>
    array (
      'name' => 'Leo Bloom',
      'username' => 'leo',
      'friend' => NULL,
      'job' => 'Accountant',
    ),
    'job' => 'Swindler',
  ),
  'job' => 'Accountant',
)

The print_r() and var_export() functions accept a second argument which, if set to true tells the functions to return the string representation of the variable rather than printing it.

To capture the output from var_dump(), however, you need to use output buffering:

1
2
3
4
ob_start();
var_dump($user);
$dump = ob_get_contents();
ob_end_clean();

This puts the results of var_dump($user) in $dump.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Sorting an Array Using a Method Instead of a Function in PHP
You want to define a custom sorting routine to order …

Sorting an Array Using a Method Instead of a Function in PHP

Picking a Random Line from a File in PHP
You want to pick a line at random from a …

Picking a Random Line from a File in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap