使 FEOF 尽快返回 true 以避免爆炸“未定义偏移”错误PHP

问题描述

我有一个打印文件内容的简单程序

<?PHP

$fp=fopen("../resources/client.txt","r"); 
$trouve=0;
$cnt=0;
while(!feof($fp)){
    $i=0;
    $cnt++;
    echo "<br>";
    $ligne=fgets($fp);
    $row=explode("|",$ligne);
    while($i<=2){
        echo $row[$i]."|";
        $i++;}
    
}

fclose($fp); ?>

它完成了工作,但在显示后我也收到了这些错误

enter image description here

这个问题有简单的解决方案吗? 提前谢谢大家。

解决方法

在空行中,您既没有 $row[1] 也没有 $row[2]

<?php

$fp=fopen("../resources/client.txt","r"); 
$trouve=0;
$cnt=0;
while(!feof($fp)){
    $i=0;
    $cnt++;
    echo "<br>";
    $ligne=fgets($fp);
    $row=explode("|",$ligne);
    if (isset($row[$i])) {
        while($i<=2){
            echo $row[$i]."|";
            $i++;
        }
    }
    
}

fclose($fp); ?>