Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,180 members, 7,818,565 topics. Date: Sunday, 05 May 2024 at 06:53 PM

What's The Differenc In These Loops FOREACH(), WHILE() - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / What's The Differenc In These Loops FOREACH(), WHILE() (680 Views)

Help Me Solve This PHP Foreach Function (2) (3) (4)

(1) (Reply) (Go Down)

What's The Differenc In These Loops FOREACH(), WHILE() by cbrass(m): 4:23pm On Jul 26, 2014
Hello guys, let's share and pass knowledge, what's the difference in these loops FOREACH, WHILE ,DO
And when is it appropriate to use them?

(Php)
Let's get the ball rollin
Re: What's The Differenc In These Loops FOREACH(), WHILE() by cbrass(m): 5:54pm On Jul 26, 2014
Guys don't do this to me nwwwww
Re: What's The Differenc In These Loops FOREACH(), WHILE() by dwebdesign(m): 6:41pm On Jul 26, 2014
The For Loop

The for loop is often the tool you will use when you want to create a loop.

The for loop has the following syntax:

for (statement 1; statement 2; statement 3) {
code block to be executed
}


</hr>

The While Loop

The while loop loops through a block of code as long as a specified condition is true.
Syntax

while (condition) {
code block to be executed
}



learn more: w3schools.com
Re: What's The Differenc In These Loops FOREACH(), WHILE() by adewasco2k(m): 7:26pm On Jul 26, 2014
^ if i am not drunk he said foreach and while loop not for loop and while loop.


personally i use any loop that willl get the work done easily, this are the things i consider:

if i am to loop over and array the Foreach is my first bet...you can use for loop and while loop also but foreach is more simpler.
example:

to loop over and array,

$users = array('name'=>'wilson', 'age'=>32, 'gender'=>'male');

if i am to loop with foreach then i do:

foreach($users AS $user){
echo $user['name'];
echo $user['age'];
echo $user['gender'];
}

for same looping if i am to use for loop, i doubt if its possible with an associative array but if its and indexed array like:

$users = array('wilson', 32,'male');

then i can do:


$x = count($users)
for($i = 0; $i < $x; ++$i) {
echo $users[$i];
}

I really dont know if there is a way to loop an associative array with a for loop

While loop comes handy when you want to keep executing a block of code as long as a condition is true, a place where most people use it is when they are getting data from a database query, example:

$result = $mysqli->query("SELECT * FROM users WHERE status = 1 AND is_a_programmer= true);

while ($row = $result->fetch_array($result)) {
echo $row[username];
echo $row[email];
}

or using while loop to loop through an array:

$users = array('wilson', 32,'male');

$i = 0;
$x = count($users)
while ($i < $x) {
echo $users[$i];
}

for do while loop, this is just same with while loop only that your block of code get executed once regardless of if the condition is true or false, after the first time it runs, the condition is then checked then if true...it runs again, else it doesnt.

I cant remember, but i was working on a javascript application and the only loop which will work fine for me was a do while loop.

so in summary they all amost do the same thing just they way they do it that differs and that all depends on what you want to do, most times you just choose any one that you want but some times maybe only a specific method will work for you
Re: What's The Differenc In These Loops FOREACH(), WHILE() by cbrass(m): 8:11am On Jul 27, 2014
Am so greatfull for all your response especially that of adewasco , I for one think the WHILE LOOP and the FOREACH LOOP have little differences, the reason am saying this is I ran into a problem 2days ago and am yet to figure the perfect answer.

I want students to answer questions from a table "questions" after that question is answered another one comes in, and another one comes in...on and on and on, but where I have an issue is the questions the student has answered keep coming back again when all questions has been answered, its starts from the top again. What I want to do is if it has finished echoing out the questions for that particular student it should stop and not start all over again..any ideas on how to solve that Have tried a lot but none seems working
Re: What's The Differenc In These Loops FOREACH(), WHILE() by GodMode: 8:38am On Jul 27, 2014
You created an infinite loop. That means u are the person creating different monikers and asking for help. You've been doing this for over 2 days.
Re: What's The Differenc In These Loops FOREACH(), WHILE() by Nobody: 10:36am On Jul 27, 2014
dwebdesign: The For Loop

The for loop is often the tool you will use when you want to create a loop.

The for loop has the following syntax:

for (statement 1; statement 2; statement 3) {
code block to be executed
}


</hr>

The While Loop

The while loop loops through a block of code as long as a specified condition is true.
Syntax

while (condition) {
code block to be executed
}



learn more: w3schools.com
Can't even give w3sch00ls a real link back? sad guyz learn@ http://naijazoom.com
Re: What's The Differenc In These Loops FOREACH(), WHILE() by cbrass(m): 1:50pm On Jul 27, 2014
GodMode: You created an infinite loop. That means u are the person creating different monikers and asking for help. You've been doing this for over 2 days.

Me When did I ask for help here..I don't remember when I asked for help about loops. just want all to learn one or two things here
Re: What's The Differenc In These Loops FOREACH(), WHILE() by micodon(m): 10:46pm On Jul 27, 2014
cbrass: Am so greatfull for all your response especially that of adewasco , I for one think the WHILE LOOP and the FOREACH LOOP have little differences, the reason am saying this is I ran into a problem 2days ago and am yet to figure the perfect answer.

I want students to answer questions from a table "questions" after that question is answered another one comes in, and another one comes in...on and on and on, but where I have an issue is the questions the student has answered keep coming back again when all questions has been answered, its starts from the top again. What I want to do is if it has finished echoing out the questions for that particular student it should stop and not start all over again..any ideas on how to solve that Have tried a lot but none seems working




Hmmmm. First thing to do is to select all questions and store them as an associative array.

Now to display the questions, loop through the associative array. MAKE SURE THAT AFTER ECHOING THE QUESTION, YOU USE THE array_shift METHOD ON THE ASSOCIATIVE ARRAY.
Re: What's The Differenc In These Loops FOREACH(), WHILE() by cbrass(m): 11:16pm On Jul 27, 2014
micodon:




Hmmmm. First thing to do is to select all questions and store them as an associative array.

Now to display the questions, loop through the associative array. MAKE SURE THAT AFTER ECHOING THE QUESTION, YOU USE THE array_shift METHOD ON THE ASSOCIATIVE ARRAY.

Thanks for you response , I actually figure another way out. I did a LEFT JOIN on the table "questions" and "answered" so I counted the colums I want and just wrote some if conditions and that did it grin

(1) (Reply)

Good Web Designer Needed In Owerri / Cash Giveaway Trends On Nigeria's Bloggers / Can You Design A Simple Page Like This

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 26
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.