loop help...[solved]

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
pioneer
Forum Newbie
Posts: 3
Joined: Mon Mar 13, 2006 8:44 am

loop help...[solved]

Post 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
Last edited by pioneer on Tue Mar 14, 2006 11:37 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
pioneer
Forum Newbie
Posts: 3
Joined: Mon Mar 13, 2006 8:44 am

Post 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?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
Post Reply