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 Build a Simple PHP Application Listing Inventory Items Displaying All Products

Erick Bongo
Erick Bongo
8,539 Points

Array to string conversation Notice

I get this error and am not sure why if someone could help

When I follow the tutorial I get an array to string conversion notice but Randy is able to display a list of arrays, I cant see anything wrong with my code so am not sure why this is happening ?

<?php

$products = array();
$products[101] = array(
        "Type" => "T-Shirt",
        "Brand" => "Nike",
        "Colour" => "Red",
        "img" => "<img src='img/nikeRed.jpg'>"
        );
$products[102] = array(
        "Type" => "T-Shirt",
        "Brand" => "Addidas", 
        "Colour" => "Blue",
        "img" => "<img src='img/addidasBlue.jpg'>"
        );
?>
<!DOCTYPE html>
<html>
    <head>
        <title>Products Page</title>
        <link type="text/css" rel="stylesheet" href="css/style.css"></link>
    </head>
    <body>
        <div>
            <ul>
                <?php foreach ($products as $product) {?>
                    <li><?php echo $product; ?></li>
                    <?php } ?>
            </ul>
        </div>
    </body>
</html> 

3 Answers

It's because when you say echo $product;, $product is an array. Randy must have his error logging configured to hide those errors, which is why he doesn't get them. Even though Randy doesn't get the same error you do, he still isn't getting any meaningful data since you shouldn't echo an array. He changes it to work correctly later in the lesson.

You can configure error logging if you open php.ini and look for the line that starts with error_reporting. You can open php.ini from the xampp control panel. Click on the Config button for Apache, and select php.ini.

Erick Bongo
Erick Bongo
8,539 Points

Hi thanks for the reply, that's really useful to know, I'll give it a try later :)

Erick Bongo
Erick Bongo
8,539 Points

I managed to get it all working, cheers for the help.