php / php/php-src

Explicilty passing default value for escape in SplFileObject::fgetcsv changes outcome

Offen
#22,156 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Extension: spl Status: Verified
Vorherrschende Sprache
C
Sterne
40.4k
Forks
8.1k
Ø Merge
2 T. 13 Std.
Gemergte PRs (30 T.)
96

Beschreibung

Description

The following code:

<?php

$file = new SplFileObject('php://memory', 'rw+');

$file->fwrite("foo;bar;baz");
$file->seek(0);

$file->setCsvControl(';', "\n");

while (!$file->eof()) {
    $line = $file->fgetcsv(escape: '\\');
    var_dump($line);
}

Resulted in this output:

array(1) {
  [0]=>
  string(11) "foo;bar;baz"
}

But I expected this output instead:

array(3) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
  [2]=>
  string(3) "baz"
}

For context, I am currently working on migrating a codebase from 8.2 to 8.4, and thought the safest option to keep existing behavior for fgetcsv calls would be to explicitly provide the current value of the default parameter, considering it will change in the next major version. I understand WHY it was implemented like this, but it is still a weird outcome. Ideally, the paramters for the method fgetcsv should be nullable, and default to null instead, so that the behavior is consistent with the way user land PHP functions and methods with default values for parameters work.

PHP Version
PHP 8.4.16 (cli) (built: Jan 13 2026 01:36:09) (NTS)
Copyright (c) The PHP Group
Built by https://github.com/docker-library/php
Zend Engine v4.4.16, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.16, Copyright (c), by Zend Technologies
Operating System

No response

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie am Einstiegspunkt SplFileObject::fgetcsv und reproduzieren Sie das Beispiel mit setCsvControl(';', "\n") unter expliziter Übergabe eines Escape-Werts. Untersuchen Sie, ob nullable Parameter dafür sorgen können, dass ausgelassene und explizit angegebene Standardwerte konsistent sind, und überprüfen Sie anschließend, dass die CSV in die angeforderten drei Felder geparst wird.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, php
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.