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

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Can we have more than 3 expression?

The video doesn't clear

geoffrey
geoffrey
28,736 Points

Could you give us more details please ? What do you mean by more than 3 expressions exactly !?

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

I mean we can put more than 3 expression in the loop on not?

Angel Lozano Jimenez
Angel Lozano Jimenez
7,255 Points

No, Thanitsak. It seems like no one really answered your question, but the answer is no. From PHP syntax, to many other languages like C, within a for-loop, you can only have the three expressions within the conditions. Your counter, your comparison and your increment, that is about it.

4 Answers

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Thanitsak Leuangsupornpong !

You might want to have a look at http://php.net/manual/en/control-structures.for.php :

for (expr1; expr2; expr3)
    statement

"...Each of the expressions can be empty or contain multiple expressions separated by commas. In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part..."

There are some nice examples illustrating this, too.

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Thanks for answer,and that mean we can put only 3 expression in the loop right?

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Do you know that why,when I put the <?php?> tag in the workspace or .php file,when I view the code,with the little eye Icon.It can't load the resource error.With all my workspace,I am feel confuse.Any Idea Please?

Is there a code block that you're trying to execute which requires more than 3 expressions? If yes, please do share it for us to get a better picture of what exactly you're trying to achieve with the loop. Thanks.

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Now,I have no require for more than 3 expression,but I just want to know,that it can or not.More than 3?Thanks you very much that you want to help.

I have never seen any for/while/do while loop with more than three expressions. I'm not sure whether you can have more than 3 expressions or not but then again, I've never come across a problem which required more than 3 expressions.

Thanitsak Leuangsupornpong
Thanitsak Leuangsupornpong
7,490 Points

Thank you very much for your answer!The normal is 3 expression.

Guilherme Oliveira
Guilherme Oliveira
3,463 Points

Maybe I am a bit late, but I will give it a try.

So, the regular expression on the for loop is - for(expr1; expr2; expr3) - there are just three expressions. Of course you have the option not to use one of them, for(;expr2;expr3) or for(;;expr3) are valid loops (you can play with it), even for(;;;) is a valid loop if you want an infinite loop.

While you can choose not to use all of the expressions, you can't add a fourth expression, but an expression can be composed by many evaluations, for example for($i = 0, $j = 0 ; $i < 10 or $j < 12; $i++, $j++) - try playing with it and echoing $i and $j