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 Introducing Arrays

Yan Liu
Yan Liu
10,552 Points

About ($letters as $letter) loop

Hi there,

As this part, learned

foreach ($letters as $letter) { }

But, why it is $letters as $letter ? I mean, where is the $letterS?

letters.php
<?php



echo "My favorite ";
echo "2";
echo " letters are these: ";
echo "AB";
echo ".";

?>

1 Answer

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

When we use arrays, it's customary to refer to the entire array as the plural of whatever each individual items are. So, if you have an array called $students, you would use a foreach loop to iterate through them like this:

<?php

$students = array("John", "Jane", "Glinda");

foreach ($students as $student) {
  echo "Student Name: " . $student;
}

?>

In this case, $students is a defined variable that holds a specific array. In the foreach loop, however, the variable $student is only a placeholder for the specific array item that is being echoed.