Differences Between a While Loop and a for Loop in PHP

Differences between a while loop and a for loop in PHP?

Can you? Yes, certainly. But whether or not you should is an entirely different question.

The for loop is more readable in this scenario, and is definitely the convention you'll find used within virtually every language that has looping directives. If you use the while loop, people are going to wonder why you didn't use a for loop.

What's the difference between while() and for()?

The difference is that the do while loop executes at least once because it checks for the loop condition while exiting. While is a entry controlled loop and do while is a exit control loop. Whereas in do while loop it will enter the loop and will then check for the condition.

while loop - used for looping until a condition is satisfied and when it is unsure how many times the code should be in loop

for loop - used for looping until a condition is satisfied but it is used when you know how many times the code needs to be in loop

do while loop - executes the content of the loop once before checking the condition of the while.

for loop vs while loop vs foreach loop PHP

which one is better for performance?

It doesn't matter.

what's the criteria to select a loop?

If you just need to walk through all the elements of an object or array, use foreach. Cases where you need for include

  • When you explicitly need to do things with the numeric index, for example:
  • when you need to use previous or next elements from within an iteration
  • when you need to change the counter during an iteration

foreach is much more convenient because it doesn't require you to set up the counting, and can work its way through any kind of member - be it object properties or associative array elements (which a for won't catch). It's usually best for readability.

which should be used when we loop inside another loop?

Both are fine; in your demo case, foreach is the simplest way to go.

Difference between a for loop and for while nested with list()

Yes, there's a difference if you look at it from algorithm point of view.

The first one has the time complexity of O(n) and the second one O(n2).

So, the first one is more efficient.

The difference between loops

For loop and While loops are entry condition loops. They evaluate condition first, so the statement block associated with the loop won't run even once if the condition fails to meet

The statements inside this for loop block will run 10 times, the value of $i will be 0 to 9;

for ($i = 0; $i < 10; $i++)
{
# code...
}

Same thing done with while loop:

$i = 0;
while ($i < 10)
{
# code...
$i++
}

Do-while loop is exit-condition loop. It's guaranteed to execute once, then it will evaluate condition before repeating the block

do
{
# code...
}
while ($flag == false);

foreach is used to access array elements from start to end. At the beginning of foreach loop, the internal pointer of the array is set to the first element of the array, in next step it is set to the 2nd element of the array and so on till the array ends. In the loop block The value of current array item is available as $value and the key of current item is available as $index.

foreach ($array as $index => $value)
{
# code...
}

You could do the same thing with while loop, like this

while (current($array))
{
$index = key($array); // to get key of the current element
$value = $array[$index]; // to get value of current element

# code ...

next($array); // advance the internal array pointer of $array
}

And lastly: The PHP Manual is your friend :)

while loop inside for loop PHP

Just add mysqli_data_seek after the while loop:

for($i=0; $i<$count; $i++){
echo "COUNT: ".$i."<br>";
while($data=mysqli_fetch_array($res)) {
echo "INSIDE WHILE".$i."<br>";
}
mysqli_data_seek($res, 0);
}

Difference between an If statement and While loop

An if statement checks if an expression is true or false, and then runs the code inside the statement only if it is true. The code inside the loop is only run once...

if (x > y)
{
// this will only happen once
}

A while statement is a loop. Basically, it continues to execute the code in the while statement for however long the expression is true.

while (x > y)
{
// this will keep happening until the condition is false.
}

When to use a while loop:

While loops are best used when you don't know exactly how many times you may have to loop through a condition - if you know exactly how many times you want to test a condition (e.g. 10), then you'd use a for loop instead.



Related Topics



Leave a reply



Submit