我可能发现了一个错误,如果是这样,我也会报告这个错误?
这就是我所拥有的:
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
我是否发现了一个错误,或者我是否在第一种方式做错了什么,并在第二种方式中纠正错误?
如果我确实发现了一个错误,我也会报告错误。
PHP v5.3.3在Ubuntu 10.10上运行CLI版本谢谢。
答案 0 :(得分: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";
我怀疑你的操作顺序错误。
答案 1 :(得分: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";