php - 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
解决方案
原来空白正在改变函数输出的时间,我需要修剪参数并且浏览器在打印之前已经默认修剪了这些值:
str_accur(trim($trgStr), trim($mtchTo), false);
推荐阅读
- azure-functions - InvalidSignature:令牌的签名无效 - Azure 函数
- javascript - 浏览器和邮递员中的错误 401 未授权 ok
- javascript - 从自定义类型创建列表/数组
- python - 使用 opencv 保存裁剪的视频
- json - 创建一个字符串来调用带有角度 html 文件中的变量的 json 对象
- linux - 将多个 excel 文件合并到一个 excel 工作簿中,但使用 bash 脚本将不同的工作表合并
- python - 为什么我可以用 int(s[0]) + int(s[1]) 而不是 int(s[0] + s[1]) 添加数字?
- php - 根据 Woocommerce 中购物车中的运输类别价格项目更改运输类别
- html - 带有一个条件选项的两个 Select 元素(下拉菜单)
- angular - 如何为 mat-select 做一个包装器,在里面将是 mat-option 的另一个包装器,并且在另一个组件中使用它