This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Functions, Part I Forum

View Course » View Exercise

474 points
B6c1667376b7e1538c7ad04682031f4a?s=140&d=retro
Submitted by
Aaron Gomez
over 3 years ago

Why doesn't my code print out the elements in my array, instead of "array?"

So I know it is unnecessary for the exercise but I wanted to see it work. Here is my code:

<?php
    // Create an array and push 5 elements on to it, then 
    // print the number of elements in your array to the screen
        $friends= array("tori", "Nessa");
        array_push($friends, "Mark");
        array_push($friends, "Ariel");
        array_push($friends, "Brayden");
        array_push($friends, "Luke");
        array_push($friends, "Aaron");

        echo $friends;

        echo count($friends);
    ?>

when I entered echo $friends, I expected it to return everything in my array. But instead it returned the word array. Why? Iv'e tried everything I can think of. Again I know it isn't necessary but plz help.


0 votes

permalink

What if you try print_r($friends); ?

450 points
5240e713abf821aa28001257_703076796
Submitted by
Robinvb
over 3 years ago

1 Comment

Picture Magayane over 3 years ago

print_r will print elements and their position. He just want to print names in the array.


0 votes

permalink

instead of echo $friends;

try: print join(",", $friends);

251 points
Picture
Submitted by
Magayane
over 3 years ago