2015-07-19 125 views
0

我剛剛進入PHP編程,並在我的for循環中出現錯誤。當我把它剪掉時,代碼運行良好。for循環中的錯誤?

<?php 

// Utils 
function contains($needle, $haystack) { 
    return strpos($haystack, $needle) !== false; 
} 


$client_ip = $_SERVER['REMOTE_ADDR']; 

$iphitsfile = fopen("iphits.txt","r"); 
$iphits = fgets($iphitsfile,1000); 
fclose($iphitsfile); 

echo $iphits; 

$ips = split(" ", $iphits); 

for($ip_and_hits : $ips) { 
    echo "$ip_and_hits"; 
    $ip = split("-", $ip_and_hits); 
    $hits = split("-", $ip_and_hits); 

    if($ip == $client_ip) { 
     $hits = $hits + 1; 

     $iphits = str_replace($ip_and_hits,$ip."-".$hits." ",$iphits); 

     $iphitsfile = fopen("iphits.txt","w"); 
     fwrite($iphitsfile, $iphits); 
     fclose($iphitsfile); 

     break; 
    } 

} 


?> 
+2

不要辱罵,讓過去我們的質量過濾器。參加[旅遊]並訪問[問],瞭解我們對這裏帖子的期望。 – rene

回答

5

你寫什麼,for($ip_and_hits : $ips) {就是Java的語法。

在PHP中,你需要使用一個foreach循環:

foreach ($ips as $ip_and_hits) { 
0

您的循環被錯誤地使用。這是一個錯誤的語法。

使用線的下面的代碼:

foreach ($ip_and_hits as $ips) { 
+2

這個答案是什麼給出了另一個答案? – rene