尝试在PHP中使用多个数学公式计算一个值。

huangapple go评论65阅读模式
英文:

Trying to calculate a value from multiple mathematical formulas in PHP

问题

Sure, here's the translated content from your code:

我是PHP的新手,您将在下面的代码中看到原因。我试图将一个字符串转换为仅包含数字,然后根据其位置将每个数字相乘,然后将所有数字相加到一个变量中,并最终除以11。但最终得到了一个太大的值。

一切都始于获取一个包含17个字母和数字的字符串,它将仅转换为数字:

我已经输出了结果,到目前为止一切都很顺利!然后是“破损”的部分。对于数字所在的每个位置,它将与特定数字相乘,例如:

  • 位置0需要乘以8
  • 位置1需要乘以7
  • 位置2需要乘以6...

然后将所有数字相加在一起,然后除以11。

我在纸上进行了计算,得到了一个太大的数字。还有一个我从哪个字符串开始的示例:VRZUA5FX29J276031,应该得到42.363636...,但最终得到了94405436.6363...

我知道我的代码有点混乱,但任何帮助都会让我开心!

英文:

I'm new to PHP you will see why in the code below. I'm trying to convert a string to only numbers, then multiply every number according to its position and then sum all up to one variable and divide it by 11 in the end. But end up getting a too big of a value in the end.

It all starts with getting a string of 17 letters and numbers, which will be converted only to numbers:

<?php
    session_start();
    


    if(!isset($_POST['submit']))
    {

        //$_SESSION['count']=0;

    }
    ?>
    <form action="" method="POST">
  <i> VIN-nummer: </i><input type="text" name="fvin">
  <input type="submit" value="Beräkna" name="submit">
  <br></br>
</form>

<?php
$fvin = $_POST['fvin'];

$l2n = array(
    'A'=>'1',
    'B'=>'2',
    'C'=>'3',
    'D'=>'4',
    'E'=>'5',
    'F'=>'6',
    'G'=>'7',
    'H'=>'8',
    'J'=>'1',
    'K'=>'2',
    'L'=>'3',
    'M'=>'4',
    'N'=>'5',
    'P'=>'7',
    'R'=>'9',
    'S'=>'2',
    'T'=>'3',
    'U'=>'4',
    'V'=>'5',
    'W'=>'6',
    'X'=>'7',
    'Y'=>'8',
    'Z'=>'9'
);

$keys = array_keys($l2n);
$values = array_values($l2n);
$evin = str_replace($keys, $values, $fvin);

I have echoed the results and so far so good! Then for the "broken" part. For every position a number is in, it will be multiplied with a specific number e.g:

  • Position 0 needs to be multiplied with 8
  • Position 1 needs to be multiplied with 7
  • Position 2 needs to be multiplied with 6...
$fvin0 = substr($evin, 0, 0). $fvin0 *=8;
$fvin1 = substr($evin, 1, 1). $fvin1 *=7;
$fvin2 = substr($evin, 2, 2). $fvin2 *=6;
$fvin3 = substr($evin, 3, 3). $fvin3 *=5;
$fvin4 = substr($evin, 4, 4). $fvin4 *=4;
$fvin5 = substr($evin, 5, 5). $fvin5 *=3;
$fvin6 = substr($evin, 6, 6). $fvin6 *=2;
$fvin7 = substr($evin, 7, 7). $fvin7 *=10;
$fvin8 = substr($evin, 8, 8);
$fvin9 = substr($evin, 9, 9). $fvin9 *=9;
$fvin10 = substr($evin, 10, 10). $fvin10 *=8;
$fvin11 = substr($evin, 11, 11). $fvin11 *=7;
$fvin12 = substr($evin, 12, 12). $fvin12 *=6;
$fvin13 = substr($evin, 13, 13). $fvin13 *=5;
$fvin14 = substr($evin, 14, 14). $fvin14 *=4;
$fvin15 = substr($evin, 15, 15). $fvin15 *=3;
$fvin16 = substr($evin, 16, 16). $fvin16 *=2;

Every number will be multiplied with a number except position 8.

Then sum everything together up and divide it by 11.

$fvin += ($fvin0 + $fvin1 + $fvin2 + $fvin3 + $fvin4 + $fvin5 + $fvin6 + $fvin7 + $fvin8 + $fvin9 + $fvin10 + $fvin11 + $fvin12 + $fvin13 + $fvin14 + $fvin15 + $fvin16)/11;


if(isset($_POST['submit']))
{
    echo $fvin;
    echo "<br>";
    
}

I've done the math on paper and I get too big of a number. Also here's an example of what string I start with: VRZUA5FX29J276031, and should end up with 42,363636... but end up with 94405436.6363...

I know my code is a bit messy but any help would make my day!

答案1

得分: 1

你不应该将$fvin<num> *= multiplier连接到每个值上。由于你尚未为这些$fvin变量分配任何值,所以你正在将0与$evin的子字符串连接起来。你应该将子字符串与这些乘数相乘。

substr()的第二个参数是子字符串的长度,而不是子字符串的结束索引。这些应该都是1。

$fvin0 = substr($evin, 0, 1) * 8;
$fvin1 = substr($evin, 1, 1) * 7;
$fvin2 = substr($evin, 2, 1) * 6;
$fvin3 = substr($evin, 3, 1) * 5;
$fvin4 = substr($evin, 4, 1) * 4;
$fvin5 = substr($evin, 5, 1) * 3;
$fvin6 = substr($evin, 6, 1) * 2;
$fvin7 = substr($evin, 7, 1) * 10;
$fvin8 = substr($evin, 8, 1);
$fvin9 = substr($evin, 9, 1) * 9;
$fvin10 = substr($evin, 10, 1) * 8;
$fvin11 = substr($evin, 11, 1) * 7;
$fvin12 = substr($evin, 12, 1) * 6;
$fvin13 = substr($evin, 13, 1) * 5;
$fvin14 = substr($evin, 14, 1) * 4;
$fvin15 = substr($evin, 15, 1) * 3;
$fvin16 = substr($evin, 16, 1) * 2;
英文:

You shouldn't be concatenating $fvin&lt;num&gt; *= multiplier to each value. Since you haven't assigned anything to any of those $fvin variables yet, you're multiplying 0, and then appending that 0 to the substring of $evin. You should be multiplying the substring by those multipliers.

The second argument to substr() is the length of the substring, not the index of the end of the substring. These should all be 1.

$fvin0 = substr($evin, 0, 1) * 8;
$fvin1 = substr($evin, 1, 1) * 7;
$fvin2 = substr($evin, 2, 1) * 6;
$fvin3 = substr($evin, 3, 1) * 5;
$fvin4 = substr($evin, 4, 1) * 4;
$fvin5 = substr($evin, 5, 1) * 3;
$fvin6 = substr($evin, 6, 1) * 2;
$fvin7 = substr($evin, 7, 1) * 10;
$fvin8 = substr($evin, 8, 1);
$fvin9 = substr($evin, 9, 1) * 9;
$fvin10 = substr($evin, 10, 1) * 8;
$fvin11 = substr($evin, 11, 1) * 7;
$fvin12 = substr($evin, 12, 1) * 6;
$fvin13 = substr($evin, 13, 1) * 5;
$fvin14 = substr($evin, 14, 1) * 4;
$fvin15 = substr($evin, 15, 1) * 3;
$fvin16 = substr($evin, 16, 1) * 2;

huangapple
  • 本文由 发表于 2023年6月12日 14:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定