Jump to content

If statement inside an if statement


19spartan94

Recommended Posts

Hey guys,

 

I am currently making a game and i have a piece of code that needs an IF statement inside another IF statement... any help would be appreciated a lot here is the code

 

if ($nerve >= 6)
$random = (rand()%5);

if ($random == 0)
{
mysql_query("UPDATE users SET experience = '$exp2' WHERE Username = '$username'");
mysql_query("UPDATE users SET cash = '$cash3' WHERE Username = '$username'");
include "navbar.php";
print "<br>";
print "You searched the bins for cash and a passer-by felt sorry for you and gave you $5 and now have $cash3 and $exp2 experience!";
echo "<br><img src='images/5dollars.jpeg'>";
}

elseif ($random == 1)
{
mysql_query("UPDATE users SET experience = '$exp2' WHERE Username = '$username'");
mysql_query("UPDATE users SET cash = '$cash2' WHERE Username = '$username'");
include "navbar.php";
print "<br>";
print "In no time at all you found $10 and now have $$cash2 and $exp2 experience!";
echo "<br><img src='images/10dollars.jpeg'>";
}

elseif ($random == 2)
{
mysql_query("UPDATE users SET experience = '$exp2' WHERE Username = '$username'");
mysql_query("UPDATE users SET cash = '$cash5' WHERE Username = '$username'");
include "navbar.php";
print "<br>";
print "After searching for what seems like hours you found $1 and now have $5 and $exp2 experience!";
echo "<br><img src='images/1dollar.jpeg'>";
}

elseif ($random == 3)
{
mysql_query("UPDATE users SET experience = '$exp2' WHERE Username = '$username'");
mysql_query("UPDATE users SET cash = '$cash4' WHERE Username = '$username'");
include "navbar.php";
print "<br>";
print "You searched the bins for cash and found $2 and now have $4 and $exp2 experience!";
echo "<br><img src='images/2dollars.jpeg'>";
}

elseif ($random == 4)
{
include "navbar.php";
print "<br>";
print "You searched the bins but all you find is a slice of stale bread...";
echo "<br><img src='images/stale bread.jpeg'>";
}
}

{while ($nerve <= 5)
include "navbar.php";
print "You need to have 6 or more nerve to do this crime, sorry.";}

 

Edit (Daniel0): Please do not use annoying titles and please wrap your code in either

 or [code] tags.[/size][/color]
Link to comment
https://forums.phpfreaks.com/topic/140378-if-statement-inside-an-if-statement/
Share on other sites

You could at least tell us where you wnt those nested ifs

 

The syntax is pretty easy

 

if ($condition1) {  //outer if
  //some code to do when $condition1 == TRUE
  if($condition2) { // inner if
    //code to do when both $condition1 and $condition2 are true
  } else {   //outer else 
    //some other code
  }
  //code outside inner if
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.