Jump to content

Please explain "FOR" loops


CyberShot
Go to solution Solved by gizmola,

Recommended Posts

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?

Link to comment
Share on other sites

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 by Jacques1
Link to comment
Share on other sites

  • Solution

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.

  • Like 2
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.