首页 > 解决方案 > function in php returns different result with same parameters in loop and outside of loop

问题描述

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

标签: php

解决方案


原来空白正在改变函数输出的时间,我需要修剪参数并且浏览器在打印之前已经默认修剪了这些值:

str_accur(trim($trgStr), trim($mtchTo), false);

推荐阅读