dpbz14739 2014-05-04 13:39
浏览 22
已采纳

总是假的,为什么? [关闭]

I have a loop:

for ($p=1; $p<=$ncarts; $p++) {
    $contador_pontos_cartela[$p] = 0;
    for ($g=1; $g<=$quantidade_sorteada; $g++) {
       foreach ($numeros_cartela[$p] as $w) {
           echo "Valor de w: {$w} <br>";
           echo "Valor de sorted: {$sorted[$g]} <br>";
           if ($w == $sorted[$g]){
              echo "truue";
              $contador_pontos_cartela[$p] += 1;
           }
       }
    }
}

And

$ncarts = 3; 
$quantidade_sorteada = 3; 
$numeros_cartela[1] = array(1, 3); 
$numeros_cartela[2] = array (5, 7); 
$numeros_cartela[3] = array(9, 11); 
$sorted[1] = 1; 
$sorted[2] = 3; 
$sorted[3] = 5; `

I inserted that echo $w and echo $sorted[$g] to see if them values were correct. And the aswer is; YES. The output of that echo is:

Valor de w: 1 
Valor de sorted: 1 
Valor de w: 3 
Valor de sorted: 1 
Valor de w: 1 
Valor de sorted: 3 
Valor de w: 3 
Valor de sorted: 3 
Valor de w: 1 
Valor de sorted: 5 
Valor de w: 3 
Valor de sorted: 5 
Valor de w: 5 
Valor de sorted: 1 
Valor de w: 7 
Valor de sorted: 1 
Valor de w: 5 
Valor de sorted: 3 
Valor de w: 7 
Valor de sorted: 3 
Valor de w: 5 
Valor de sorted: 5 
Valor de w: 7 
Valor de sorted: 5 
Valor de w: 9 
Valor de sorted: 1 
Valor de w: 11 
Valor de sorted: 1 
Valor de w: 9 
Valor de sorted: 3 
Valor de w: 11 
Valor de sorted: 3 
Valor de w: 9 
Valor de sorted: 5 
Valor de w: 11 
Valor de sorted: 5 

In other words, I have 3 positive results. Where $w is equal $sorted (on the 1,1; 3,3; and 5,5) But if ($w == $sorted[$g]) never becomes true... You know, I have a echo inside if; echo "truue"; and this never is printed.

Can anybody see the problem?

  • 写回答

1条回答 默认 最新

  • dongmijgnnq0118 2014-05-04 14:17
    关注

    The output of your var_dump():-

    string(3) "1 " string(1) "1" string(3) "3 " string(1) "1" string(3) "1 " string(1) "3" string(3) "3 " string(1) "3" string(3) "1 " string(1) "5" string(3) "3 " string(1) "5" string(3) "5 " string(1) "1" string(3) "7 " string(1) "1" string(3) "5 " string(1) "3" string(3) "7 " string(1) "3" string(3) "5 " string(1) "5" string(3) "7 " string(1) "5" string(3) "9 " string(1) "1" string(4) "11 " string(1) "1" string(3) "9 " string(1) "3" string(4) "11 " string(1) "3" string(3) "9 " string(1) "5" string(4) "11 " string(1) "5"
    

    Shows that you are not getting clean data/input. You are trying to compare "1 " with "1". Take a look at where these values come from and ensure that they are cleaned up. eg use trim() to remove any unwanted white space or cast them to integers.

    This problem often occurs when using data that has originally come from user input and has not been properly filtered.

    I would recommend you do that with your source data, but as a quick fix, you could do it in your loop using trim().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了