how to use foreach

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
srichandar
Forum Newbie
Posts: 6
Joined: Thu Apr 02, 2009 9:36 am

how to use foreach

Post by srichandar »

hi all,

how this statement "for($i=1; $i<=10; $i++)" can be written using foreach loop


thanks in advance,
sridhar
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Re: how to use foreach

Post by dethron »

Code: Select all

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
To find this example
1.) Go to http://www.php.net
2.) Search for "foreach"
3.) Then you got it.

P.S: Newbies are a little lazy i guess :)) Nice to be back here again ;)
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: how to use foreach

Post by php_east »

srichandar wrote:how this statement "for($i=1; $i<=10; $i++)" can be written using foreach loop
foreach is for array. is you wish to rewrite you need $i as an array. so there is no equivalent.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Re: how to use foreach

Post by dethron »

php_east wrote:foreach is for array. is you wish to rewrite you need $i as an array. so there is no equivalent.
then put all these numbers into an array. i am not saying it is the logical way, but if you look at the example i gave you will see that you can use it like the way he asked.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: how to use foreach

Post by php_east »

uh, never mind.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: how to use foreach

Post by McInfo »

Code: Select all

for  ($i = 1; $i <= 10; $i++)
is equivalent to

Code: Select all

foreach (range(1, 10) as $i)
But the first is more efficient.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 1:42 pm, edited 1 time in total.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: how to use foreach

Post by socket1 »

I believe you want to use while for that.

while ($i=1; $i<=10; $i++) {
}
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re

Post by McInfo »

socket1 wrote:I believe you want to use while for that.

while ($i=1; $i<=10; $i++) {
}
Did you test your code before you posted it?

http://php.net/while

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 1:43 pm, edited 1 time in total.
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: how to use foreach

Post by socket1 »

Alright it doesn't work I was suspecting that but if if you want code that works this is where i've used such a statement:

Code: Select all

$i = 500;
while ($i>1 && $i<100) {
    $i = mt_rand(0, 101);
}
It only stops if mt_rand generates a 0 or a 101.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: how to use foreach

Post by php_east »

McInfo wrote:

Code: Select all

for ($i = 1; $i <= 10; $i++)
is equivalent to

Code: Select all

foreach (range(1, 10) as $i)
But the first is more efficient.
ah, so there is an equivalent apparently. one learns something new every day. thanks.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re

Post by McInfo »

The while loop that is equivalent to

Code: Select all

for ($i = 1; $i <= 10; $i++)
is

Code: Select all

$i = 1;
while ($i <= 10) {
    $i++;
}
Edit: This post was recovered from search engine cache.
Post Reply