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 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?