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

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.