jump to navigation

Three Ways to Output the State of Your Array-Variables on the Screen While Debugging PHP Code March 25, 2012

Posted by Tournas Dimitrios in PHP.
trackback

During debugging of  PHP-code  you might need to find out what the contents are of a specific array- variable .  The var_dump() , print_r() and var_export() functions can all be used to gain insight as to the state of your array-variables at a particular point in your code . The difference between them is what you get as output . Let’s analyze each function separately :

  • var_dump :   What is particular to the var_dump() output is that the data type is indicated along with the length of the value  (i.e. string(7) “Classic”) .  Hence the name, the variable information is literally “dumped” out to the screen .
    <?php
    
    $a = array( "Drink"=> "Beer", "Candy"=>"Cookies", "Foods"=>array("macaroni", "Bread", "Meat","Fruits"=>array("Red"=>"Strawberry","Yellow"=>"Banana","Green"=>"Apples")), "Music Styles"=>array("Soft"=>"Classic", "Heavy"=>"Hard-Rock","Ethnic"=>"Country-Roc"));
    
    echo "<pre>" ;
    var_dump($a) ;
    echo "</pre>" ;
    
    /*
    Output :
    array(4) {
      ["Drink"]=>
      string(4) "Beer"
      ["Candy"]=>
      string(7) "Cookies"
      ["Foods"]=>
      array(4) {
        [0]=>
        string(8) "macaroni"
        [1]=>
        string(5) "Bread"
        [2]=>
        string(4) "Meat"
        ["Fruits"]=>
        array(3) {
          ["Red"]=>
          string(10) "Strawberry"
          ["Yellow"]=>
          string(6) "Banana"
          ["Green"]=>
          string(6) "Apples"
        }
      }
      ["Music Styles"]=>
      array(3) {
        ["Soft"]=>
        string(7) "Classic"
        ["Heavy"]=>
        string(9) "Hard-Rock"
        ["Ethnic"]=>
        string(11) "Country-Roc"
      }
    }
    */
    
    ?>
    
  • print_r : The main purpose of print_r() is that it presents the variable information in a human-readable format . You can also pass an optional Boolean argument: “True” to surpres output to the page . This is helpful when you want to assign the value of the array to another variable . Remember that print_r() will move the array pointer to the end. Use reset() to bring it back to beginning .
    <?php
    
    $a = array( "Drink"=> "Beer", "Candy"=>"Cookies", "Foods"=>array("macaroni", "Bread", "Meat","Fruits"=>array("Red"=>"Strawberry","Yellow"=>"Banana","Green"=>"Apples")), "Music Styles"=>array("Soft"=>"Classic", "Heavy"=>"Hard-Rock","Ethnic"=>"Country-Roc"));
    
    echo "<pre>" ;
    print_r($a) ;
    echo "</pre>" ;
    
    /*
    Output :
    Array
    (
        [Drink] => Beer
        [Candy] => Cookies
        [Foods] => Array
            (
                [0] => macaroni
                [1] => Bread
                [2] => Meat
                [Fruits] => Array
                    (
                        [Red] => Strawberry
                        [Yellow] => Banana
                        [Green] => Apples
                    )
    
            )
    
        [Music Styles] => Array
            (
                [Soft] => Classic
                [Heavy] => Hard-Rock
                [Ethnic] => Country-Roc
            )
    
    )
    */
    
    ?>
    
  • var_export : It’s output is very much like print_r() , except that  it returns  valid PHP code (literally PHP code) which can be reused  in a php file (useful when you generate an array which should be used in another php code where you can easily copy paste and use )  This can be very useful , depending on what you do with the return value .
    <?php
    
    $a = array( "Drink"=> "Beer", "Candy"=>"Cookies", "Foods"=>array("macaroni", "Bread", "Meat","Fruits"=>array("Red"=>"Strawberry","Yellow"=>"Banana","Green"=>"Apples")), "Music Styles"=>array("Soft"=>"Classic", "Heavy"=>"Hard-Rock","Ethnic"=>"Country-Roc"));
    
    echo "<pre>" ;
    var_export($a) ;
    echo "</pre>" ;
    
    /*
    Output :
    array (
      'Drink' => 'Beer',
      'Candy' => 'Cookies',
      'Foods' =>
      array (
        0 => 'macaroni',
        1 => 'Bread',
        2 => 'Meat',
        'Fruits' =>
        array (
          'Red' => 'Strawberry',
          'Yellow' => 'Banana',
          'Green' => 'Apples',
        ),
      ),
      'Music Styles' =>
      array (
        'Soft' => 'Classic',
        'Heavy' => 'Hard-Rock',
        'Ethnic' => 'Country-Roc',
      ),
    )
    */
    
    ?>
    

Instead of an array , we could also use an object and get it’s   insight view . But to get this functionality the Class should have an __toString() magic method implemented into it’s structure .

Advertisements

Comments»

1. golddog - April 2, 2012

I simply could not depart your web site prior to suggesting that I extremely loved the standard information an individual provide to your visitors? Is gonna be again often to investigate cross-check new posts

2. No Hands - April 11, 2012

I do consider all of the concepts you have offered in your post. They’re very convincing and will definitely work. Nonetheless, the posts are too quick for beginners. Could you please prolong them a bit from subsequent time? Thanks for the post.

tournasdimitrios1 - April 11, 2012

@NoHands
Thanks for your comments .
I would be glad to answer to any technical question you might have .

3. heal - April 13, 2012

Great website. Plenty of helpful info here. I am sending it to some pals ans additionally sharing in delicious. And obviously, thank you for your sweat!

4. quick - April 17, 2012

We are a group of volunteers and opening a brand new scheme in our community. Your site offered us with useful information to paintings on. You’ve performed an impressive activity and our entire group will likely be thankful to you.

5. site - May 25, 2012

Dude! This blog is cool! How did you make it look like this ?

tournasdimitrios1 - May 25, 2012

My blog is hosted on WordPress.com and the theme is Regulus . If you need help , let me know 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s