HoTDaWg Posted December 29, 2008 Share Posted December 29, 2008 hey there, ive just been working on getting the for loops syntax down but i cant get this simple script to work. in summary, this script counts from the start integer to the stop integer (or vice versa depending upon the users selection) by a select number of increments for a selected number of times. but for some reason, it counts correctly but does not repeat the sets of numbers... any ideas:S <?php error_reporting(E_ALL); print<<<HTML <html> <head> <title>Activity Seven- ask for a low integer, high integer, increment to count by, how many times to count up or down, and whether to count from HL or LH</title> </head> <body> <form method="post"> <input type = "text" name = "startint" value="start integer"><br> <input type = "text" name = "stopint" value="stop integer"><br> <input type = "text" name = "incr" value= "increment"><br> <input type = "text" name = "method" value="hl or lh"><br> <input type = "text" name = "reps" value= "repetitions"><br> <input type = "submit"> </form> HTML; if ((isset($_POST['startint'])) && (isset($_POST['stopint'])) && (isset($_POST['incr'])) && (isset($_POST['method'])) && (isset($_POST['reps']))) { $startint = $_POST['startint']; $stopint = $_POST['stopint']; $incr = $_POST['incr']; $method = $_POST['method']; $reps = $_POST['reps']; for ($c = 1; $c <= $reps; $c++) { if ($method == "lh") { for ($g = &$startint ; $g <= $stopint ; $g += $incr) { print "<br>$c) " . $g; } } else { for ($g = &$stopint ; $g >= $startint ; $g -= $incr) { print "<br>$c) " . $g; } } } } ?> any help would be greatly appreciated thanks. Link to comment https://forums.phpfreaks.com/topic/138800-php-repetitions-for-loop-error/ Share on other sites More sharing options...
btherl Posted December 29, 2008 Share Posted December 29, 2008 Try removing the "&" symbols from your two inner loops: for ($g = &$startint; ... These ones ^^^ Link to comment https://forums.phpfreaks.com/topic/138800-php-repetitions-for-loop-error/#findComment-725786 Share on other sites More sharing options...
HoTDaWg Posted December 30, 2008 Author Share Posted December 30, 2008 thanks a lot the script works great now out of curiosity what influence did the ampersands have in the inner loops? Link to comment https://forums.phpfreaks.com/topic/138800-php-repetitions-for-loop-error/#findComment-725792 Share on other sites More sharing options...
flyhoney Posted December 30, 2008 Share Posted December 30, 2008 The ampersand means you are referencing the location of $g instead of the value of $g. This means that you were actually saying "$g and $startint point to the same value", and whenever you incremented $g, $startint also incremented. Thus, every rep but the first would skip the for statement since $g was already <= $stopint. Link to comment https://forums.phpfreaks.com/topic/138800-php-repetitions-for-loop-error/#findComment-725806 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.