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

Var_dump not display all variables

Only the first two variables are dumped out into my code, but I need all three.

<?php

$projects = array();
$projects[] = array(
    "name" => "MultiVision Infographic",
    "img" => "images/infographic.jpg",
    "picture" => "images/slice/infographic-slice.jpg",
    );

$projects[] = array(
    "name" => "Magazine Spread",
    "img" => "images/slice/magazine-slice.jpg",
    "picture" => "images/wired-magazine-page1.jpg",
    );

$projects[] = array(
    "name" => "Photography",
    "img" => "images/slice/michael-slice.jpg",
    "picture" => "images/michael-photography.jpg",
    );

$projects[] = array(
    "name" => "Idenity Design",
    "img" => "images/slice/skype-slice.jpg",
    "picture" => "images/skype-stationary.jpg",
    );
?>

<div id="work">
    <h1>Work</h1>
        <ul id="gallery">
            <?php foreach($projects as $project){
                    echo "<li>";
                    echo '<a href= '. $project["picture"] . ' >';
                    echo '<img src="'. $project["img"] . '" alt="' . $project["name"] . '">';
                    echo "</a>";
                    echo "</li>"; 
                }
            ?>
        </ul>      

            <?php var_dump($projects); ?>
</div>

5 Answers

on every third variable there you added a comma that shouldn't be there, since no other is coming after :) it should be like:

$projects[] = array(
    "name" => "Idenity Design",
    "img" => "images/slice/skype-slice.jpg",
    "picture" => "images/skype-stationary.jpg"
    );
?>

Thanks for a quick response. But I have tried the array with and without a comma at the end. I even switched around the order of the variables, yet only "name" and "img" display each time.

I tried your code, and haven't been able to reproduce the problem. Is that all of your code? Do you have some part of the code to populate that array? It seems that you have something preventing that line from executing somehow. Here's my result of the var_dump:

    array(1) {
  [0]=>
  array(3) {
    ["name"]=>
    string(23) "MultiVision Infographic"
    ["img"]=>
    string(22) "images/infographic.jpg"
    ["picture"]=>
    string(34) "images/slice/infographic-slice.jpg"
  }
}

Sorry ^^ I wish I could help you more

Camilla I have updated my code in the original message, of what I have on my site. Thanks for your help in advance.

This is how the code runs

<a href= ><img src="images/slice/infographic-slice.jpg" alt="MultiVision Infographic"></a>

[0]=> array(2) { ["name"]=> string(23) "MultiVision Infographic" ["img"]=> string(34) "images/slice/infographic-slice.jpg" }

I just pasted your code in and added a html <pre> tag to make it easier to read.

[0]=>
  array(3) {
    ["name"]=>
    string(23) "MultiVision Infographic"
    ["img"]=>
    string(22) "images/infographic.jpg"
    ["picture"]=>
    string(34) "images/slice/infographic-slice.jpg"
  }

So, comparing mine to yours, we can see that the values that should go in "picture", goes into "img" instead. For some reason when you run the code, that portion is getting skipped. The coded you pasted for me has no errors, since I didn't fix anything and it runs perfectly for me.

My guess, considering that the code you're running is the same one you pasted is that it is a server side hicup. If you are running the PHP server on a localhost, MAMP/XAMP shouldn't consume your whole computer CPU. But you could check your CPU usage when your server is running jsut to make sure everything is ok. Also try to consider shutting down other memory/processing heavy programs when the server is running.

One thing I like to do when debuging weirdness like that is to start a new file and paste in the code piece by piece, checking to see which part causes it to bug. It happened to me before that removing a comment out of the file (comments are ignored by code, aren't they? they shouldn't matter...) would completly break my code... To this date I can't quite explain it, but leaving the comment there it would work, and erasing the comment would break it, go figure! So, I can't quite figure why you're having problems, but the code you pasted WORKS. That's for sure :)