this is my code
<?php 
    $file = fopen("employee.txt","r");
    $under_40=0;
    $salary_sum=0; 
    $emplist=[];
    $total_employees=0;
    echo "Names of all employees whose names end with son : <br/>";
    while(!feof($file))
      {
        $total_employees++;
        $fline=fgets($file);
        list($name, $age, $dept, $salary)= explode(':',$fline);
        if(preg_match("/son$/",$name))
        {
            echo $name."<br/>"; 
        }
        if($age<40)
        {
            $under_40++;
            $salary_sum +=$salary;
            if($salary>40000 )
            {
                $emplist[$name]=$salary;
            }
        }
      }
    if($total_employees>0)
    {
        if($under_40>0)
        {
                $percent=100*$under_40 / $total_employees;
                echo "<br/>Percent of employees under 40 is :".$percent."%<br/>";
                $avg= $salary_sum/$under_40;
                echo "<br/>Averege salary of employees under 40 is :$".$avg."<br/>";
                if(array_keys($emplist))
                {
                    echo "<br/>Sorted list of employees under 40, with salries > \$40000 <br/>";
                    ksort($emplist);
                    echo "<table><tr><td>Name</td><td>Salary</td></tr>";
                    foreach ($emplist as $key=>$value)
                    {
                        echo "<tr><td>".$key."</td><td>\$".$value."</td></tr>";
                    }
                    echo"</table>";
                }
                else
                {
                        echo"There are no employees under 40 who earned over \$40000 <br/>";
                }
        }
        else
        {
                echo"There are no employees under 40 <br/>";
        }
    }
    else
    {
            echo"There are no employees ";
    }
    fclose($file);
?>
expected output is Names of all employees whose names end with son: Johnson Edison Morrison
Percent of employees under 40 is :40%
Averege salary of employees under 40 is :$31000
Sorted list of employees under 40, with salries > $40000 Name Salary Harry $43000
but am getting like this
Names of all employees whose names end with son :
Notice: Undefined offset: 3 in C:\xampp\htdocs\lab2.php on line 12
Notice: Undefined offset: 2 in C:\xampp\htdocs\lab2.php on line 12 Johnson Edison Morrison
Percent of employees under 40 is :50%
Averege salary of employees under 40 is :$21000
Sorted list of employees under 40, with salries > $40000 Name Salary Harry $43000
 
    