for循环出错?

wou*_*ter 0 php for-loop

我刚刚进入PHP编程,我的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;
    }

}


?>
Run Code Online (Sandbox Code Playgroud)

Glo*_*del 5

你写的for($ip_and_hits : $ips) {是类似Java的语法.

在PHP中,您需要使用foreach循环:

foreach ($ips as $ip_and_hits) {
Run Code Online (Sandbox Code Playgroud)