$testCase="";
$strength="";
$i="";$j="";$sum=0;
$submit=$_POST['submit'];
if(!empty($submit))
{
$testCase=$_POST['TestCase'];
$strength=$_POST['strength'];
}
else
{
die("Please enter the Number of <strong>test case</strong><br/> and <strong> Strength of audience</strong>");
}
while($testCase>0)
{
for($i=1;$strength;$i=$i+1)
{
$user[$i]=$i+1;
}
//finding the total number of stickers
for($j=1;$j<$strength;$j=$j+1)
{
$sum=$sum+$user[$j];
}
}
echo "The total number of <strong>Stickers</strong> are = " . $sum;
Asked
Active
Viewed 77 times
0
-
2Few problems here. Why is `$testCase` initialized as a string when you are comparing it with 0? – David Nov 03 '14 at 13:43
-
5You have an infinite loop, `$testCase` never changes inside the loop. – jeroen Nov 03 '14 at 13:44
-
$testCase is not initialized to anything. – aps240 Nov 03 '14 at 13:51
-
Thanx for the help , so silly of me for not noticing it. – aps240 Nov 03 '14 at 13:52
-
This should not be closed as a duplicate for memory issues, the problem is unrelated to the actual amount of memory available. – jeroen Nov 03 '14 at 15:41
2 Answers
1
Because you do this: while ($testCase > 0) {
and do not decrase the $testCase
ever. Also, it's better if you put your loops into your if condition.
And you do not need to create $strength
variable. Try this:
$sum = 0;
if (!empty($_POST["submit"]) && !empty($_POST["testCase"]) && !empty($_POST["strength"])) {
$testCase = $_POST["testCase"];
while ($testCase > 0) {
for ($i = 1; $i < $_POST["strength"];$i++) {
$user[$i] = $i + 1;
}
for ($j = 1; $j < $_POST["strength"]; $j++) {
$sum = $sum + $user[$j];
}
//Here you need to decrase testcase
$testCase--;
}
} else {
die("Please enter the Number of <strong>test case</strong><br/> and <strong> Strength of audience</strong>");
}
echo "The total number of <strong>Stickers</strong> are = " . $sum;
NOTE: In PHP arrays are always started with 0, if you do not specify keys directly. Check your for loops for this.

vaso123
- 12,347
- 4
- 34
- 64