I have the following code on PERL:
$Mid = 123;
$csum=0;
$csum+=$_ foreach split //,$Mid;
$csum%=10;
It calculates $csum
based on $Mid
which is a number
How to do the same on PHP?
I have the following code on PERL:
$Mid = 123;
$csum=0;
$csum+=$_ foreach split //,$Mid;
$csum%=10;
It calculates $csum
based on $Mid
which is a number
How to do the same on PHP?
The following should do the trick:
$mid = 123;
$csum = 0;
foreach(str_split($mid) as $m){ // Loop $mid by character
$csum+= $m; // Add current $character ($m) to our $csum
}
$csum = $csum % 10; // Modulo by 10
echo $csum; // 6
Working PHP, and to compare, the working Perl version