php subtracting a variable from a variable to get the value -


im learning php , cant piece of code work, beginning php , cant figure out. online validator reports no issues found sets of infinite loop me. can 1 tell me why $eggusage not being subtracted $eggs.

thank :-)

<?php      $eggs = 12;     $eggusage = 0;      while($eggs - $eggusage > 0) {     $eggusage = rand(0, 1, 2, 3, 4);     $eggs = $eggs - $eggusage;      echo "you have {$eggs} left";     }      if ($eggs < 4) {     $eggusage = rand(0, 1, 2, 3, 4);     $eggs == $eggs - $eggusage;      echo "you out. buy eggs!";     }      echo "<p>congratulations, out of eggs.</p>"   ?> 

rand() expects 2 parameters, have given 4 in code. try rand( 0, 4 ).

also, don't forget ; in last echo, , had little mistake in line $eggs == $eggs - $eggsusage, should 1 =.

this code snippet works fine me:

    $eggs = 12;     $eggusage = 0;      while($eggs - $eggusage > 0) {        $eggusage = rand(0,4);        $eggs = $eggs - $eggusage;         echo "you have {$eggs} left";     }      if ($eggs < 4) {        $eggusage = rand(0,4);        $eggs = $eggs - $eggusage;         echo "you out. buy eggs!";     }      echo "<p>congratulations, out of eggs.</p>" ; 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -