x
This website is using cookies. We use cookies to ensure that we give you the best experience on our website. More info. That's Fine
HPC:Factor Logo 
 
Latest Forum Activity

fragenmensch Page Icon Posted 2005-11-05 8:48 AM
#
Avatar image of fragenmensch
Factorite (Elite)

Posts:
207
Location:
DE-DE
Status:
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...

Edited by fragenmensch 2005-11-05 9:00 AM
 Top of the page
Snappy! Page Icon Posted 2005-11-06 5:36 AM
#
Avatar image of Snappy!
H/PC Elder

Posts:
1,712
Location:
New Mexico, US
Status:
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 Italics

Edited by Snappy! 2005-11-06 5:38 AM
 Top of the page
abyssknight
abyssknight Page Icon Posted 2005-11-06 9:02 AM
#
Status:
Yep, Snappy has it.

Basically $var{$x} means "the character at X index in the variable var".

Had to use this at work once... Hehe.
 Top of the page
fragenmensch Page Icon Posted 2005-11-06 9:12 AM
#
Avatar image of fragenmensch
Factorite (Elite)

Posts:
207
Location:
DE-DE
Status:
Thanks. This is a part of a checksum generator I found in the web, it creates vaild checksums for an german ID card and I want to port that from PHP to Perl, because a PHP Port to CE dosen't exist ATM.

Marco

PS: What do I need for doing this?
 Top of the page
Jump to forum:
Seconds to generate: 0.125 - Cached queries : 60 - Executed queries : 9