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 PHP Basics (Retired) PHP Conditionals & Loops For Loops

Ben Os
Ben Os
20,008 Points

php: I have no idea why I get a "Unidentified variable" error here, plus an infinite loop...

I work locally with notepad++ on purpose so I didn't copy anything from the teacher so he might have preliminary code pieces that I miss, and thus can't get the following code to work:

for($counter = 0; $border = 10; $adder + 1){
    echo "<li>" . $counter . "</li>";
}

4 Answers

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,349 Points

I am not sure why you want to use a $adder variable or a $border variable. Using a single variable the loop should work fine.

for($counter = 0; $counter < 10; $counter++){
    echo "<li>" . $counter . "</li>";
}
Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,349 Points

Hi Ben, In the code you have shown, $adder is not defined anywhere, this is your unidentified variable. Also in your loop there is no conditional statement, which is what is causing an infinite loop.

Here is the traditional way of doing a For Loop:

for($counter = 0; $counter < 10; $counter++) {}
Ben Os
Ben Os
20,008 Points

Hi, I tried to work with the traditional way but I got these errors thus I changed... Anyway indeed I want to work in this traditional way...

Can you detail about the $adder variable? Even when I defined it outside and did the following I still encounter the same problems:

$adder = 1;
for($counter = 0; $border < 10; $adder++){
    echo "<li>" . $counter . "</li>";
}
Ben Os
Ben Os
20,008 Points

Indeed! It worked when I used a single variable as you you showed Dale! Thanks!

The reason I used three different variables was that after I saw the for loop is built using variables in php I used the terminology I knew from a Introduction to JS I had (there I didn't use variables I just this names in my mind for the three modular parts of the loop in JS but anyway, in PHP I'll work with one variable as you showed.

Since I am very very new to both languages (and to programming in general) I want to ask (hope it's okay), how come the php for loop knows how to treat each representation of the variable inside it? Is it built-in functionality of the php for loop?

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,349 Points

The loop has 3 parts. First is the variable declaration, second is the variable test condition, and third is the variable modifier. The first part initializes $counter to 0 and then is not used again. Then a loop begins which tests if $counter is less than 10, and if not, $counter is incremented by 1. The code in the brackets is executed each time through each increment. Finally when it hits 10, the condition fails, and it exits the loop. This is a common programming loop used in PHP, JavaScript, and probably any other programming language.