Questions on Check VB6 functions for PHP

Asked

Viewed 73 times

1

I need to pass some VB6 codes for PHP but I have a question of what this code does:

Private Function SomarValor(v As String) As Double
Dim x As Byte, resultado As Double
For x = 1 To Len(v)
resultado = resultado + CDbl(Mid(v, x, 1))
Next
SomarValor = resultado
End Function

This function SomarValor() receives values such as 0202827610, if anyone can help.

I have tried to look up the meaning of these internal functions as CDbl, Mid, but I couldn’t understand.

  • Yes it is possible the mid() is equivalent to substr()

  • right, but what this does For x = 1 To Len(v) and this function "Cdbl", would you explain to me, please?

  • 1

    If I understand correctly, he reads 0202827610 character by character and sum the result.

1 answer

3


The code takes a string 0202827610 is made a for that extracts a character from that string as mid() and has its value converted to double with Cdbl() which finally adds up the value.

A way to convert this code to PHP would be like this:

$str = '0202827610';

$arr = str_split($str);
$total = 0;
foreach($arr as $item){
    $total += $item;
}

echo $total;

Basically the str_plit() transform the string into an array and it is iterated and summed inside the foreach.

  • Perfect friend, thank you. I never had contact with VB6 so I was lost in what it did there. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.