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 Foreach Loops

Brian Platt
Brian Platt
5,009 Points

Loops in real life

Hey, So I just finished learning about loops in PHP (but to my understanding it's a pretty universal coding concept) and just wanted to get a feel for how they work in real life. Is this like when we take code challenges here on Treehouse and we see the "Bummer..." text if we miss a question? Do the programmers put every possible correct answer as a 'elseif' statement at the end of the loop and you can only proceed if you input one of those? Or is this all beyond where I am? Thanks!

1 Answer

Justin Iezzi
Justin Iezzi
18,199 Points

Loops have an almost infinite amount of uses. A loop is useful for any sort of task that requires iteration.

Is this like when we take code challenges here on Treehouse and we see the "Bummer..." text if we miss a question?

No. If there is an actual error with the loop, the code will break and the compiler will let you know, if there is one. But more often than this you'll run into logic errors, where the code runs fine but it isn't exactly doing what you intended for it to do. You can create issues that you don't realize are issues until months down the road. So in real life, there isn't always something to tell you that you're doing something wrong. There are practices and methods to combat this, which you will eventually learn.

Do the programmers put every possible correct answer as a 'elseif' statement at the end of the loop and you can only proceed if you input one of those?

It sounds like you're referring to a very specific loop / set of conditions. The programmer will make the exact amount of 'elseif' statements the program requires, there really isn't a set standard. For when the possible conditions are too great in number, then there are other ways of going about it without cluttering your code, but any method will essentially do the same thing. I don't think it's terribly common to have that many conditions for one statement, though. Remember that the uses of these tools, such as loops and conditions, are basically infinite. Existing loops and conditions that you'll run across will come in all sorts of different shapes, sizes, and configurations.

You're still learning the concepts of how programming works. These sort of concepts will become much clearer to you as you continue learning. Try not to concern yourself with how coding works in "the real" world for now, focus on learning the tools and how you can use these tools to make the program do what you want. That is essentially real programming.

Keep learning, and good luck!

Brian Platt
Brian Platt
5,009 Points

Awesome, thanks. Sorry, I meant in the context in Treehouse itself with this partโ€” Do the programmers put every possible correct answer as a 'elseif' statement at the end of the loop and you can only proceed if you input one of those? It's just hard for me to wrap my head around PHP and programming in general without real life examples.