Jump to content

Multiple Looper


KingOfHeart

Recommended Posts

I want to create a loop where it would basically create an output of data that would look like this

 

....

....

....

....

 

4x4

 

for($y; $y < $y2; $y ++)

{

echo "y:" . $y . "<br>";

for($x; $x < $x2; $x ++)

{

echo "x:" . $x . "<br>";

}

}

 

It's not looping the way I want, like it just loops once each. Is using for the wrong method or do I need to use while in this case?

Link to comment
Share on other sites

Is using for the wrong method or do I need to use while in this case?

 

Generally, any for loop will have the following structure:

for (startingStatement; condition; iterationStatement) {
    body;
}

 

That can be written like this using a while loop:

startingStatement;
while (condition) {
    body;

    iterationStatement;
}

 

So it will never really matter if you use a while loop instead of a for loop. It's just two ways of writing the same thing. Thought it might be useful to know.

 

Actually, in a for loop, all the things within the parentheses are optional, so you can also do this:

for (; {
    something;
}

Link to comment
Share on other sites

Are you a new programmer, and asking what all these loops for, and why are there so many.

 

 

as you been told, does not matter what loop you use, but as long as you no, there other loops for php.

 

for loop.

while loop.

do while loop.

 

as you get better at coding your start to use all the loops as possable....

 

most common loop is the FOR loop my opinion

 

 

There also the foreach array loop out there....

 

 

 

 

Link to comment
Share on other sites

as you been told, does not matter what loop you use

 

Not necessarily. Iterating over an associate array would look ugly using e.g. a while loop.

 

Assuming you have the following array:

$array = array(
    'foo' => 'bar',
    'test' => 'hello',
);

 

Iterating over it using a while loop would look something like this:

$arrayKeys = array_keys($array);
$arrayValues = array_values($array);
$i = 0;
$arraySize = sizeof($array);

while ($i < $arraySize) {
    echo $arrayKeys[$i] . ' => ' . $arrayValues[$i] . PHP_EOL;
    
    ++$i;
}

 

Compare that to the following (which is much easier to read):

foreach ($array as $key => $value) {
    echo $key . ' => ' . $value . PHP_EOL;
}

 

 

Another example would be creating an infinite loop (that you of course break out of somehow within the body). Using a while loop you would do this:

while (true) { doSomething; }

and the for loop equivalent would look like this:

for (; { doSomething; }

 

The for loop in this case would be most efficient (though not by much) because it doesn't need to evaluate a statement all the time.

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.