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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!