Im having a problem that my while loop is not finishing even when the conditions have been meet.

$turn = "0";
while ($player['health_n'] > "0" OR $opponent['health_n'] > "0" AND $turn < "25"){
     $turn = $turn+1;
     $hitfor = $youatk / $oppdef;
	 if ($hitfor <= "0"){
	 echo "$turn. $opponent[name] blocked.<br />";
	 }else{
	 echo "$turn. You hit $opponent[name] for $hitfor damage.<br />";
	 mysql_query("UPDATE members SET health_n=health_n-$hitfor WHERE id='$opponent[id]' ")or die(mysql_error());
	 }
	 $turn = $turn+1;
	 $atkfor = $oppatk / $youdef;
	 if ($atkfor <= "0"){
	 echo "$turn. You blocked.<br />";
	 }else{
	 echo "$turn. $opponent[name] hit you for $atkfor damage.<br />";
	 mysql_query("UPDATE members SET health_n=health_n-$atkfor WHERE id='$player[id]' ")or die(mysql_error());
	 }
}

I am getting the data from the database outside of the while loop; if this is any help.

Recommended Answers

All 6 Replies

try this:

while ($player['health_n'] || $opponent['health_n'] && $turn < 25)

If a variable is zero it can be thought of as FALSE, and also you were looking for $turn that was greater than the string 25. see if that helps.

That sorta worked except it was checking the right thing, I wanted it to perform the loop if the player health was above 0 OR opponent health was above 0 OR turns where below 25. So something like this

while ($player['health_n'] > "0" || $opponent['health_n'] > "0" || $turn < 25)

Except it does not work. This happens whenever I do == or > or => or althing like that with the player/opponent health and so your previous example not using any conditional for the arrays worked but it did not create the desired effect.

you are checking the value of strings and NOT integers. stop putting the numbers in quotes, you dont need to do that.

Also, this snippet you posted is different, you have three || statements not 2 || and an && statemnt. is that what you wanted to do all along?
Have you tried echoing out the variables in your script, before after and during your loop??

Yes having 3 || is what I wanted.

EDIT: Ok I've just tried removing the "" and it still carry's on after the turns have reached above 25 and the health is below 0. i'll show you my updated code

while ($player['health_n'] > 0 || $opponent['health_n'] > 0 || $turn < 25){
     $turn = $turn+1;
     $hitfor = $youatk / $oppdef;
	 if ($hitfor <= "0"){
	 echo "$turn. $opponent[name] blocked.<br />";
	 }else{
	 echo "$turn. You hit $opponent[name] for $hitfor damage.<br />";
	 
	 mysql_query("UPDATE members SET health_n=health_n-$hitfor WHERE id='$opponent[id]' ")or die(mysql_error());
	 }
	 $turn = $turn+1;
	 $atkfor = $oppatk / $youdef;
	 if ($atkfor <= "0"){
	 echo "$turn. You blocked.<br />";
	 }else{
	 echo "$turn. $opponent[name] hit you for $atkfor damage.<br />";
	 mysql_query("UPDATE members SET health_n=health_n-$atkfor WHERE id='$player[id]' ")or die(mysql_error());
	 
	 }
}

Basically this loop is supposed to run until the health of someone reaches 0 or turns get higher than 25 (Whichever comes first.) And then after this I want to give the player bonuses for winning so I have:

if ($player['health_n'] <= "0"){

Too check if he lost and

}elseif ($opponent['health_n'] <= "0"){

If he won (Also a if condition to check if the 25 turn limit was reached and so to set it as a draw.)

But whats happening is that while loop above isn't stopping, it carrys on and on.

while ($player['health_n'] > "0" OR $opponent['health_n'] > "0" AND $turn < "25"){

is from your first post. I guess I was confused at what your intentions were...

Must have not been thinking when I was coding it, sorry for misleading ya and wasting your time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.