Skip to content

SplFileObject::fgetcsv does not return null for last line in CSV mode #5823

@jjanvier

Description

@jjanvier

I'm reading CSV files using SplFileObject class and fgetcsv method. Below code does not work as intended in HHVM 3.8.0 meaning fgetcsv does return false on the LAST line. In PHP 5.6.7-1 the last line returns null.

With the following CSV file

row11;;row13
row21;row22;row23

and the following code

$aFile = new SplFileObject ($aPath);
$aFile->setFlags(SplFileObject::SKIP_EMPTY | SplFileObject::READ_AHEAD | SplFileObject::DROP_NEW_LINE | SplFileObject::READ_CSV);
$aFile->setCsvControl(';');

for($i = 0; $i < 3; $i++) {
    $row = $aFile->fgetcsv();
    var_dump($row);
}

Result for PHP

array(3) {
  [0] =>
  string(5) "row11"
  [1] =>
  string(0) ""
  [2] =>
  string(5) "row13"
}
array(3) {
  [0] =>
  string(5) "row21"
  [1] =>
  string(5) "row22"
  [2] =>
  string(5) "row23"
}
NULL

Result for HHVM

array(3) {
  [0]=>
  string(5) "row11"
  [1]=>
  string(0) ""
  [2]=>
  string(5) "row13"
}
array(3) {
  [0]=>
  string(5) "row21"
  [1]=>
  string(5) "row22"
  [2]=>
  string(5) "row23"
}
bool(false)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions