2011-09-07 114 views
1

我可能发现了一个错误,如果是这样的话,我该如何报告呢?想到我在PHP v5.3.3-1ubuntu9.5 CLI中发现了一个错误?

这里是我的本钱:

for ($i = 0; $i <= ($count)-1; ++$i) { 
Lots of code...more then I want to put here 
.... 
..... 
..... 
$mailto = $mailto . $i +1 . ") " . $name . " " . $location . " Type: " . $type . " Status: " . $geostatus . " Difficulty " .$difficulty . "\n" . $region . "\nHidden: " . $hidden . " Last Update: " . $lastupdate . " Last Found: " . $lastfound . "\nDistance: $distance Bearing: $bearing degrees $direction\n\n"; 
} 

echo "$mailto \n"; 

输出将是这样的:

4) Morrow Parking - RR Relic LMBT Lat: 39.35455 Lon: -84.133733 Type: Traditional Cache Status: Active Difficulty 3 
City/State: Morrow, OH Warren Co. 
Hidden: Wed June 09 2010 04:00 Last Update: Wed August 03 2011 17:43 Last Found: Wed December 31 1970 19:00 
Distance: 4.31 Bearing: 212 degrees SW 

显示我只有在列表中的最后一项。 但是,如果我只是稍微改变代码如下:

for ($i = 0; $i <= ($count)-1; ++$i) { 
Lots of code...more then I want to put here 
.... 
..... 
..... 
$cc = $i +1; 
$mailto = $mailto . $cc . ") " . $name . " " . $location . " Type: " . $type . " Status: " . $geostatus . " Difficulty " .$difficulty . "\n" . $region . "\nHidden: " . $hidden . " Last Update: " . $lastupdate . " Last Found: " . $lastfound . "\nDistance: $distance Bearing: $bearing degrees $direction\n\n"; 
} 

echo "$mailto \n"; 

我得到充分的输出,因为我希望看到:

1) Uranus - The Solar System - Lil Miami Bike Trail Lat: 39.415517 Lon: -84.103383 Type: Traditional Cache Status: Active Difficulty 1.5 
City/State: , OH Warren Co. 
Hidden: Sun October 17 2010 04:00 Last Update: Fri August 05 2011 18:25 Last Found: Wed December 31 1970 19:00 
Distance: 0.92 Bearing: 309 degrees NW 

2) 79 Lat: 39.394517 Lon: -84.096533 Type: Traditional Cache Status: Active Difficulty 2 
City/State: , OH Warren Co. 
Hidden: Sun May 30 2010 04:00 Last Update: Wed August 03 2011 17:42 Last Found: Wed December 31 1970 19:00 
Distance: 0.93 Bearing: 201 degrees S 

3) RR Relic - Oregonia Lat: 39.462583 Lon: -84.100383 Type: Traditional Cache Status: Active Difficulty 1.5 
City/State: Oregonia, OH Warren Co. 
Hidden: Sun May 02 2010 04:00 Last Update: Wed August 03 2011 17:41 Last Found: Wed December 31 1970 19:00 
Distance: 3.88 Bearing: 351 degrees N 

4) Morrow Parking - RR Relic LMBT Lat: 39.35455 Lon: -84.133733 Type: Traditional Cache Status: Active Difficulty 3 
City/State: Morrow, OH Warren Co. 
Hidden: Wed June 09 2010 04:00 Last Update: Wed August 03 2011 17:43 Last Found: Wed December 31 1970 19:00 
Distance: 4.31 Bearing: 212 degrees SW 

我有没有发现一个bug,还是我做错事的第一种方式,并在第二种方式更正?

如果我确实发现了一个错误,我该如何报告。

PHP v5.3.3在Ubuntu 10.10上运行CLI版本谢谢。

+0

听起来微不足道......但你试过'$ mailto。 ($ i + 1)。# – rlemon

回答

2

在代码的第一位,更改:

$mailto = $mailto . $i +1 . ") " . $name . " " . $location . " Type: " . $type . " Status: " . $geostatus . " Difficulty " .$difficulty . "\n" . $region . "\nHidden: " . $hidden . " Last Update: " . $lastupdate . " Last Found: " . $lastfound . "\nDistance: $distance Bearing: $bearing degrees $direction\n\n"; 

$mailto = $mailto . ($i +1) . ") " . $name . " " . $location . " Type: " . $type . " Status: " . $geostatus . " Difficulty " .$difficulty . "\n" . $region . "\nHidden: " . $hidden . " Last Update: " . $lastupdate . " Last Found: " . $lastfound . "\nDistance: $distance Bearing: $bearing degrees $direction\n\n"; 

我怀疑你刚刚的操作错误的顺序。

+0

具体来说,将一个字符串和一个整数'($ mailto。$ i)连接起来,但可能不是你的意图。但是,添加一个非数字字符串和一个整数将字符串视为0. –

+0

好的,这确实给了我期望看到的输出。谢谢你......节省一点额外的东西! :-)所以我的下一个问题是它不会仍然是一个错误,因为$ i是我的循环计数器 - – user895468

+0

不,这不是一个错误,只是你忘记了操作顺序和类型转换。你在做'$ mailto。 $ i',然后加1。当你为一个字符串添加一个int时,在它被添加到'$ i'之前,字符串被转换为一个int(零)。这就是为什么当你看到'$ i'的结果是4时,你已经把所有前面的输出变成了'0'。 – ceejayoz

1

它与运算符优先级有关。这里有一个简单的例子:

$i=1;// doesn't matter what this value is 
$s = " alpha " . $i+1 . " beta " . " gamma<br>\n"; 
echo $s; // 1 beta gamma 

$s = " alpha " . ($i+1) . " beta " . " gamma<br>\n"; 
echo $s; // alpha 2 beta gamma 

在第一种情况下的异常行为,甚至发生,如果你的猫加入到(INT)。

$s = " alpha " . (int)$i+1 . " beta " . " gamma<br>\n"; 
+0

感谢所有的信息,我的妻子在数学课,她总是问我操作的顺序,我猜我应该学会倾听自己。好吧,我没有发现错误,不好,我只是不知道我在做什么...感谢您的帮助。 – user895468

+0

不错的简单例子。 +1 – ceejayoz

相关问题