Array Trouble, calculating numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Array Trouble, calculating numbers

Post by Smudly »

Hi, I'm working on a simple calculation script that will start with two numbers (these chosen by user). The user chooses a number, and it is set for both numbers. These two values are placed inside an array. The script adds these two numbers, and gets a new value. The value that was in $array[1] moves to $array[0], and the new value is now placed inside $array[1]. This calculation keeps going one and on, adding the values inside the array. By typing in 1 for my start value, I expected my code to output: 1, 1, 3. 5, 8, 13, 21, 34, 55, etc.
However, right now it is outputting the following:
1
1
2
3
5
8
13
9
10
10
2
3
5
8
13
9
10
10
2
3
5
8
13
here is my code below. What can you see that I have done wrong? Any help appreciated.

Code: Select all

<?php
$submit = $_POST['submit'];
$base = $_POST['base'];

$array = "";
?>
<html>
<body OnLoad="document.price.base.focus();">
<center>
<form name="price" action="calculate.php" method="POST">
	Base Price: $<input type="text" name="base">
				<input type="submit" name="submit" value="Submit">
</form>
<h1>$<?php echo $base; ?></h1>
<br />
</center>
</body>
</html>

<?php
if($submit){

$array .= $base;
$array .= $base;
echo $array[0]."<br />".$array[1]."<br />";

$newvalue = $array[0] + $array[1];
$array[0] = $array[1];
$array[1] = $newvalue;
echo $newvalue."<br />";
	
$end = 0;
while ($end<1000){
	
	$number = $array[0] + $array[1];
	echo $number."<br />";
	$array[0] = $array[1];
	$array[1] = $number;

	$end += 1;
	
}
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Array Trouble, calculating numbers

Post by requinix »

You aren't using arrays.

PHP manual
Post Reply