2011-09-07 42 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

相關問題