Jump to content

Php repetitions for loop error


HoTDaWg

Recommended Posts

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

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.

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.