Jump to content

[SOLVED] ifs and else ifs


seany123

Recommended Posts

ive got so confused and i dont know which of these should be ifs and which should be else ifs...

 


<?php	
///////////////////////////////////////// AWAKE 10%//////////////////////////////////////////
if ($player->nerve >= $nerveloss && $player->awake >= $player->maxawake * 0 && $player->awake < $player->maxawake * 0.20)	
{
//sucess / fail.    
$random1 = rand(1,1000);

//Prison.
$random2 = rand(1,100);

//sucess
if($random1 <= 76){
if ($player->exp + $expgain < $player->maxexp)
    {
     //Update player (the Winner)
    $query = $db->execute("update `players` set `nerve`=?,`money`=?, `exp`=? where `id`=?", array($player->nerve - $nerveloss, $player->money + $moneygain, $player->exp + $expgain, $player->id));
    //Update enemy (the Loser)
    $query = $db->execute("update `players` set `money`=? where `id`=?", array($enemy->money - $moneygain, $enemy->id));
    //Add log message for enemy
    $logmsg = "<a href=\"profile.php?id=" . $player->username . "\">" . $player->username . "</a> mugged you and stole " . $number_format($moneygain) . "<br />";
    addlog($enemy->id, $logmsg, $db);
    //Add output for the player
    echo "You mugged " . $enemy->username . " successfully.";
    echo "<br> You gained " . $expgain . " and $" . $english_format_number = number_format ($moneygain) . "."; 
    echo "<br><br>";
    echo "<a href=\"home.php\">Back</a>";
    } 
    
else if ($player->exp + $expgain >= $player->maxexp)
    { 
    //Update player (the Winner)
    $query = $db->execute("Update `players` set `level`= `level`+?, `maxexp` =?, `exp` =?, `money` = `money` + ?, `hp`=?, `maxhp`=?,`maxenergy`=`maxenergy`+?, `energy`=?, `crimes_sucess`=?, `crimes_money`=?, `maxnerve`=? where `id`=?",array(1, $player->maxexp - $player->maxexp + $maxexpgain, $newexp, $player->money + $moneygain, $player->maxhp + 10, $player->maxhp + 10, $player->maxenergy + 1, $player->energy = $player->maxenergy, $player->crimes_sucess + 1, $player->crimes_money + $moneygain, $player->maxnerve + 1, $player->id));
    //update enemy (the loser)
    $query = $db->execute("update `players` set `money`=?, `id`=?", array($enemy->money - $moneygain, $enemy->id));
    //Add log message for enemy
    $logmsg = "<a href=\"profile.php?id=" . $player->username . "\">" . $player->username . "</a> mugged you and stole " . $number_format($moneygain) . "<br />"; 
    addlog($enemy->id, $logmsg, $db);
    //Add output for the player
    echo "You mugged " . $enemy->username . " successfully.";
    echo "<br> You gained " . $expgain . " and $" . $english_format_number = number_format ($moneygain) . "."; 
    echo "<br><br>";
    echo "<a href=\"home.php\">Back</a>";  
    }
    }  

    //Fail
if ($random1 >= 77 && $random2 >= 60){
    //update player (the Loser)
    $query = $db->execute("update `players` set `nerve`=?, `crimes_failed`=? where `id`=?", array($player->nerve - $nerveloss, $player->crimes_failed + 1, $player->id ));
    //Add log message for enemy 
    $logmsg = "<a href=\"profile.php?id=" . $player->username . "\">" . $player->username . "</a> mugged you but failed stole <br />"; 
    addlog($enemy->id, $logmsg, $db);
    //Add output for the player
    echo "<br />You failed to mug " . $enemy->username . "<br />";
    echo "<br><br>";
    echo "<a href=\"home.php\">Back</a>";
    } 
    
    //Fail & Prison
else if($random2 >= 77 && $random2 <= 59){ 
    //update player (the Loser)
    $query = $db->execute("update `players` set `nerve`=?, `prison`=?, `crimes_failed`=? where `id`=?", array($player->nerve - $nerveloss, $player->prison + $prisontime, $player->crimes_failed + 1,$player->id ));
    //Add log message for enemy 
    $logmsg = "<a href=\"profile.php?id=" . $player->username . "\">" . $player->username . "</a> mugged you but failed stole <br />"; 
    addlog($enemy->id, $logmsg, $db);
    //Add output for the player
    echo "You failed to mug " . $enemy->username . "."; 
    echo "You was sent to prison.";
    echo "<br><br>";
    echo "<a href=\"prison.php\">Prison</a>";
    }
}

 

please could someone look at this and let me know which should be which?

 

thanks

Link to comment
Share on other sites

Sometimes something we loose sight of quite easily is WHY we use something, or we forget the basics of what it does.

 

<?php
$yourName = 'sam'

if ($yourName == 'bob') {
    echo 'hi bob';
}
else if ($yourName == 'jill') {
    echo 'hi jill';
}
else {
    echo 'I dont know you';
}

//OUTPUT: I dont know you
?>

 

why you may never see

<?php
if ($random1 >= 77 && $random2 >= 60){
?>

is because of your odds...

//sucess / fail.    
$random1 = rand(1,1000);

//Prison.
$random2 = rand(1,100);

 

for testing purposes maybe bring  $random1 down to 100

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.