简体   繁体   中英

Decoding function to better understand

I am revisiting PHP and want to relearn where i lack and i found one problem, i am unable to understand the following code, where as it should output 6 according to the quiz, i got it from but i broke it down to simple pieces and commented out to better understand, according to me the value of $sum should be 4 , but what i am doing wrong, maybe my breakdown is wrong?

$numbers = array(1,2,3,4);

$total = count($numbers);
//$total = 4
$sum = 0;

$output = "";

$i = 0;

foreach($numbers as $number) {

    $i = $i + 1;
        //0+1 = 1
        //0+2 = 2
        //0+3 = 3
        //0+4 = 4


    if ($i < $total) {

        $sum = $sum + $number;

        //1st time loop = 0 < 4 false
        //2nd time loop = 0 < 1 false
        //3rd time loop = 0 < 2 false
        //5th time loop = 0 < 3 false
        //6th time loop = 4 = 4 true
            //$sum + $number
            //0 + 4
            //4
    }

}

echo $sum;

This is very basic question and might get down vote but it is also a strong backbone for people who want to become PHP developer.

You don't understand the last part in the loop. It actually goes like this now:

if($i < $total) {
    $sum = $sum + $number;
    //1st time loop: $sum is 0. $sum + 1 = 1. $sum is now 1.
    //2nd time loop: $sum is 1. $sum + 2 = 3. $sum is now 3.
    //3rd time loop: $sum is 3. $sum + 3 = 6. $sum is now 6.

    //4th time loop: it doesn't get here. $i (4) < $total (4)
    //This is false, so it doesn't execute this block.
}

echo $sum; // Output: 6

I altered your script a little so that it will print out what it's doing as it goes. I find it useful to do this kind of thing if I'm having a hard time thinking through a problem.

$numbers = array(1,2,3,4);

$total = count($numbers);
$sum = 0;

$i = 0;
$j = 0;
foreach($numbers as $number) {
    $i = $i + 1;
    echo "Iteration $j: \$i +1 is $i, \$sum is $sum, \$number is $number";
    if ($i < $total) {
        $sum = $sum + $number;
        echo ", \$i is less than \$total ($total), so \$sum + \$number is: $sum";
    } else {
        echo ", \$i is not less than \$total ($total), so \$sum will not be increased.";
    }
    echo '<br>'; // or a new line if it's CLI
    $j++;
}

echo $sum;

Lets Explain

Your initial value of $i is 0 but when you start looping you increment it by 1, so the start value of $i is 1.

When checking the condition you did't use equal sign to check for the last value whether you start value is 1. So its clear that your loop must be run for 1 less of total.

$i = 0;
foreach($numbers as $number) {
    $i += 1;
    if ($i < $total)        
        $sum += $number;
}
echo $sum;

Analysis

Step: 1 / 4
The value of $number is: 1 And The value of $i is: 1

Step: 2 / 4
The value of $number is: 2 And The value of $i is: 2

Step: 3 / 4
The value of $number is: 3 And The value of $i is: 3

When the loop again go for a check the value of $i increased by 1 and at 4. So trying to match the condition if ($i < $total) , where the value of $i and $total is equal, so it will return false. So the loop only run for 3 time.

Result

6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM