Search code examples
phpfor-loopechomodulo

why does this php code return me 13 and not 3? I'm really confused


Can someone tell me why the output of echo of this Code is 13?

$a=10;
$b=2;
$j=$a/2;
for ($i=0;$i<$j;$i++){
    if ($i % $b == 1) 
    echo "$i";
}

Solution

  • $a=10;
    $b=2;
    $j=$a/2;  //which will be 5
    for ($i=0;$i<$j;$i++){ //the loop executes 5 times 
        if ($i % $b == 1) // this condition satisfies when $i becomes 1 && 3
        echo "$i"; //1 and 3 will be printed.
    }
    

    Check The comments written in your code