fragenmensch - 2005-11-05 6:48 AM
Can anyone be so kind and explain me this bit of code?
$multipliers ="731";
$sum += $number{$i} * $multipliers{$i%3};
I can code PHP but dont know what {$i%3} means
($i is a count value in a "for"
).
Thanks,
Marco
EDIT: A char combo caused an smiley...
$i % 3 means "modulus of $i with divider 3" or something. Its the remainder after dividing $i by 3. Basically, you will get a value of 0~2, ie 0, 1, 2 depending on the value of $i.
eg,
$i = 10, $i%3 = 1
$i = 11, $i%3 = 2
$i = 12, $i%3 = 0 <== 12 is divisible by 3 giving remainder of 0
In the case of the code above, I believe
$multipliers{$i%3}
uses the value returned in '$i%3'
(0,1 or 2
) to reference the characters in the string $multiplier, in this case "731". So
Value of $i Value of $i%3 Value of $multipliers{$i%3}
3 0 7
4 1 3
5 2 1
The fragment does not contain a definition of $number, but I presume it is defined somewhere above. And in this case, its value returned depends on $i itself.
PHP supports overloading of its variable-type that is extremely flexible. So the two numerical value of $number{$i} and $multipliers{$i%3} are multiplied together and added to $sum with the '+='
The statement
$sum += $number{$i} * $multipliers{$i%3};
basically expands to
$sum = $sum + $number{$i} * $multipliers{$i%3};
Hope this clarifies your queries.
EDIT: Rephrased for clarity in
ItalicsEdited by Snappy! 2005-11-06 5:38 AM