CyberShot Posted July 19, 2017 Share Posted July 19, 2017 I have used "FOR" loops before when working on stuff. I partly understand how they work. One issue that I don't understand is when to attach the variable. For example for(i = 0; i < 10; i++){ echo $myVar[i]; ] There are times when attaching the "i" has worked and times when it doesn't. The last time I tried this, I thought it would echo a string 10 times but what it actually did was echo ten letters of the string. I just don't know understand the difference between the two. Can someone explain this to me please? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 19, 2017 Share Posted July 19, 2017 (edited) The above code isn't even syntactically valid. PHP variables always start with a dollar sign. Your question also has nothing to do with loops. It's about strings and arrays. Since you struggle with such basic concepts, I suggest you go all the way back and (re)learn PHP. There's the manual which explains all PHP types, and there are many courses for beginners (like Codecademy). I see no point in trying to come up with some kind of ruleset for when to use square brackets. You need to actually understand how PHP works. Edited July 19, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Solution gizmola Posted July 19, 2017 Solution Share Posted July 19, 2017 There are times when attaching the "i" has worked and times when it doesn't. The last time I tried this, I thought it would echo a string 10 times but what it actually did was echo ten letters of the string. I just don't know understand the difference between the two. Can someone explain this to me please? For loops are just generic conditional control structures. The php syntax was borrowed from c, and it's common to many languages. In your example there's a problem because all php variables have to start with a '$'. But if we forget about that for a second, and just consider your question, here is what happened. What you wanted (echo a string 10x) $myVar = "This is awesome"; for ($i = 0; $i < 10; $i++){ echo $myVar . '<br>'; } What you did: $myVar = "This is awesome"; for ($i = 0; $i < 10; $i++){ echo $myVar[$i] . '<br>'; } What is happening then? In this second case, what you are asking php to do, is to try and transform $myVar into an array from a string.PHP is happy to do this for you, as it already considers a string to be an array of characters. So what your code does is echo a single character from the $myVar string each time, and since $i is being incremented you get this: $myVar[0] .. then $myVar[1] .. then $myVar[2] etc. until the loop condition becomes false. In your example, it will loop 10 times, or up to 10 letters of the string: from $myVar[0] - $myVar[9]. The loop itself is going to repeat the individual instructions contained within the control structure -- { ... } until the condition becomes false. With "for loops", you just have some place holders for built in variables that it will initialize, and increment or decrement each time the loop is entered. In other words, the $i is something that you may or may not want to be using inside the loop, but it's not required. Just to echo 10 strings, there is no need for you to reference $i inside the loop at all. 2 Quote Link to comment Share on other sites More sharing options...
CyberShot Posted July 23, 2017 Author Share Posted July 23, 2017 Thank you for that answer gizmola. That was helpful. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 23, 2017 Share Posted July 23, 2017 You still need to learn the PHP basics, though. There won't always be somebody to write the code for you. 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.