this code is to sum up 1 to 10 using the arithmetic progression
The output should be as 550 code:
<?php
$total = 0;
for ($i = 1; $i <= 10; $i++) {
for ($j = 10; $j >=1 ; $j++) {
$total = $i + $j;
break;
}
}
$total = $total / 2;
print $total."<br/>";
?>
Current output: 10 Expected output: 550
and In second case I have
<?php
$list = [1, 2, 3, 4];
foreach ($list as $row)
{
switch ($row)
{
case 1:
print "This is One
";
case 2:
print "This is Two
";
case 3:
print "This is Three
";
}
}
print "
";
?>
Current output: This is One This is Two This is Three This is Two This is Three This is Three
Expected output: This is One This is Two This is Three This is Four
In my first code I want to get 550 through first program but when I run it throw wrong output i.e 122666133 and in case of second program current program throw wrong output but expected output as I mention above. So, How can I fix this issue ?Please help me.
Thank You
# Please check Below Code:
<?php
$list = [1, 2, 3, 4];
foreach ($list as $row)
{
switch ($row)
{
case 1:
print "This is One
";
break;
case 2:
print "This is Two
";
break;
case 3:
print "This is Three
";
break;
default:
print "This is Four
";
break;
}
}
print "
";
?>
UPDATE: Since you changed your question, can you explain what are you trying to do at first question?
In the first question try to use += instead of concetenation. You are simply adding 10 + i at each step to your total variable. The sum should be 110. Then you divide it by 2. So expected result is 55. I suggest you to use some print statements inside or outside of for loops to see the mistake.
Second question is already answered properly.