Page 1 of 1

loop help...[solved]

Posted: Tue Mar 14, 2006 11:17 am
by pioneer
I am passing in three different instances of the same varaiables in to a function...

i.e 2, 5 and 7($variable) and 3,4,9 ($moreV) (all stored in an array)

These are being passed in using a loop (foreach loop) to this function.

For each variable instance i pass in, i want to perform some calculations...

so this is what i have so far...

Code: Select all

<?php

function name($variable, $moreV)
{
	while ($variable = 2) {

		//do something... (print $moreV)

	}

}
?>
This causes an infinte loop and if i put a print statement in, it continues to print the variable over and over.
I simply want to use each instance one at a time.

Using an IF statement prints all instances. (i.e 3, 4, 9)

How can i perform calculations on the different instances in turn?

Cheers

Posted: Tue Mar 14, 2006 11:25 am
by JayBird
this...

Code: Select all

while ($variable = 2) {
...is always true

did you mean...

Code: Select all

while ($variable == 2) {
but i still dont know how that will solve your problem becuase if $variable = 2, you will get the infinite loop again!

Not sure what you are trying to do, but at first glance, you may be going about it the wrong way

Posted: Tue Mar 14, 2006 11:32 am
by pioneer
Yes tried that also, again it still is an infinite loop...

Is there anyway to get the first instance from a while loop and get it to stop once it has done so?

Posted: Tue Mar 14, 2006 12:16 pm
by shiznatix
dunno why this is 'solved' if you have a questions still but yes you can do somtin like this

Code: Select all

while ($var == 2)
{
    //do your code stuff
    break;
}
the break; will cause php to break out of that loop