doudundian9558 2019-03-22 19:33
浏览 40
已采纳

php中的函数在循环和循环外返回不同的结果

I have a nested loop in which the outer loop gets an item from the array and then the inner loop runs a function on each item of a different array. The problem is that inside these loops the function returns a different value for two parameters but if I just copy these values by printing it to the screen and then separately on the file it returns a different result.

I tried to print each iteration of the loop echoing the function call in a string with parameter as variables being printed and then running the function like below:

echo "str_accur($dbBookTitle[$i], {$bookDataInfo[$j][0]}, false): ";
echo str_accur($dbBookTitle[$i], $bookDataInfo[$j][0], false);

here inside the loop it shows different result:

str_accur( Cyber-Physical Systems, ApplicationAnalysisToolsforASIPDes, false): 21

As you can see the 21 is the result of the function and then I will copy that function and try it separately like this

echo str_accur("Cyber-Physical Systems", "ApplicationAnalysisToolsforASIPDes", false);

with the same parameter it is returning 9. which i expect but why in a loop it prints 21 is beyond me.

Here is the loop:

for($i = 1;$i < $dbDataLen;$i++){
    echo "from database index ({$i}): " . $dbBookTitle[$i];
    for($j = 0;$j < $bdiL;$j++){
        if($bookDataInfo[$j][0] == "False"){
            $class = 'w3-red';
        }else{
            $class = 'w3-green';
        }
        echo "<p class='$class'>bookDataInfo index ($j) : {$bookDataInfo[$j][0]}</p>";
        echo "str_accur($dbBookTitle[$i], {$bookDataInfo[$j][0]}, false): ";
        echo str_accur($dbBookTitle[$i], $bookDataInfo[$j][0], false);
        echo "<hr>";
    }
}

The actual data and the source code for str_accur are large so i don't know if should display it here but I put it on GitHub just in case. https://github.com/siyaddigital/phpFuncProblem

  • 写回答

1条回答 默认 最新

  • duanli9591 2019-03-23 03:55
    关注

    Turns out white space is changing the function output big time, I needed to trim the parameters and browser already trimming the values by default before printing them:

    str_accur(trim($trgStr), trim($mtchTo), false);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?