所選答案提供了大量的錯誤信息。我有一個理解如何使比較更嚴格使代碼工作的問題。你會認爲恰恰相反。
PHP手冊:
$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
我還在我的回答立場 - 我想我是在正確的軌道上......但我們永遠不會知道。
我相信問題出在你代碼的else
部分。
我重新格式化了代碼,這樣我就可以確保你沒有任何不匹配大括號的問題(你沒有)。
請嘗試下面的代碼。僅供參考 - 我刪除了$ id2,因爲您沒有在您提供的代碼中使用它。
echo "Looking for: {$fbID}\n";
$photos = '';
for($i=0; $i<=count($photoData['data']); $i++)
{
$id = $photoData['data'][$i]['tags']['data'][0]['id'];
echo $id . ($id == $fbID)?' matches':' DOES NOT MATCH' . "<br>\n";
if($id == $fbID)
{
$photos .= "<div style='float:left;margin:25px;'><a href='crop.php?url=".$photoData['data'][$i]['source']."'><img src='".$photoData['data'][$i]['picture']."'/></a></div>";
}
else
{
$photos = 'We cant find any photo tagged with you, go back and select another album';
break; // if you don't break, then you're possibly overwriting anything that's been added in the "true" portion of the if statement.
}
}
[編輯]
兩個循環將有助於在這裏。可能有一種方法可以將其重構爲單個循環,但這應該起作用。
echo "Looking for: {$fbID}<br>\n";
$items = array();
for($i=0; $i<=count($photoData['data']); $i++)
{
$id = $photoData['data'][$i]['tags']['data'][0]['id'];
echo $id . ($id == $fbID)?' matches':' DOES NOT MATCH' . "<br>\n";
if($id == $fbID)
{
$items[] = $photoData['data'][$i];
}
}
$photos = 'We cant find any photo tagged with you, go back and select another album';
if (count($items))
{
foreach($items)
{
$photos .= '<div style="float:left;margin:25px;"><a href="crop.php?url=' . $item['source'] . '"><img src="' . $item['picture'] . '"/></a></div>';
}
}
echo $photos;
你知道你正在改變代碼第二行'$ id'的值,對吧? –
@Emil Vikstrom $ id2,它不是$ id。 –
使用'var_dump($ id);'和'var_dump($ fbID);'來說明這一點,而不是你認爲變量包含的內容。 – mario