2015-07-19 128 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