Prince_Bert Posted April 10, 2013 Share Posted April 10, 2013 I'm learning PHP from the book, PHP & MySQL in Easy Steps but when I reached the section on breaking loops I think the author is a bit unclear about how many braces are used and therefore I cannot get the code to work as it should. The trouble is he writes it in so many stages. This is the first for loop and nested inner loop. Do I need braces here?? <?php for ( $i = 1 ; $i < 4 ; $i++ ) { for ( $j = 1 ; $j < 4 ; $j++ ) echo "Running i = $i and j = $j <br>" ; } ?> </body> </html> I assume the above is correct as it works OK. However, when I try to add the break code it just will not work however I do it - with or without curly braces, or wherever else the break statement in the code. I type it as follows: { if ( $i == 2 && $j == 1 ) echo "Breaks inner loop when i = $i and j = $j <br>" ; break ; } Can anyone help?? I have been stuck on this for nearly an entire day and have tried everything. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 10, 2013 Share Posted April 10, 2013 Without curly braces only the first line is in the loop. So your code is this: <?php for ( $i = 1 ; $i < 4 ; $i++ ) { for ( $j = 1 ; $j < 4 ; $j++ ) { echo "Running i = $i and j = $j <br>" ; if ( $i == 2 && $j == 1 ) { echo "Breaks inner loop when i = $i and j = $j <br>" ; } break ; } } ?>Just use them. It will be easier for you to read. Use them and indent. Quote Link to comment Share on other sites More sharing options...
Prince_Bert Posted April 10, 2013 Author Share Posted April 10, 2013 Thanks Jessica. That still does not display like the book says it should. I am getting Running i = 1 and j = 1 Running i = 2 and j = 1 Breaks inner loop when i = 2 and j = 1 Running i = 3 and j = 1 However, that is not showing the complete loop as it should be before and after the break. Am I using the wrong software. I have Notepad, but I am not sure whether I should have a different version for PHP. Something just isn't right. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 10, 2013 Share Posted April 10, 2013 I didn't fix your code, I showed you why it won't work. The break is outside of the loop. It will execute every time (and therefore only one time.) Quote Link to comment Share on other sites More sharing options...
Prince_Bert Posted April 10, 2013 Author Share Posted April 10, 2013 OK, can anyone show me how to fix this code. I haven't a clue and nothing I try works. I have spent hours trying different things. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 10, 2013 Share Posted April 10, 2013 (edited) If you want the break to apply when your conditional is true, you need to put it INSIDE the conditional's braces. (If it's going to work at all, break is weird when nested.) Edited April 10, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
Prince_Bert Posted April 10, 2013 Author Share Posted April 10, 2013 I have tried that numerous times. It will not happen. My issue is I don't know how many curly braces I should have for the above code. My book does not make it clear and despite Googling I cannot find a similar example. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 10, 2013 Share Posted April 10, 2013 You should have ALL of them. Quote Link to comment Share on other sites More sharing options...
Prince_Bert Posted April 10, 2013 Author Share Posted April 10, 2013 You mean all 6 including the ones you added in the above example. I think I'll need a beginners forum. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.