Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FILE_SKIP_EMPTY_LINES only works with FILE_IGNORE_NEW_LINES #18120

Open
DanielEScherzer opened this issue Mar 20, 2025 · 2 comments
Open

FILE_SKIP_EMPTY_LINES only works with FILE_IGNORE_NEW_LINES #18120

DanielEScherzer opened this issue Mar 20, 2025 · 2 comments

Comments

@DanielEScherzer
Copy link
Contributor

Description

The following code (https://3v4l.org/Ys4MU):

<?php

$filename = tempnam('/tmp', 'testing-');
file_put_contents($filename, "First\n\nSecond\n");
$lines = file($filename, FILE_SKIP_EMPTY_LINES);
echo "Just FILE_SKIP_EMPTY_LINES:\n";
var_dump( ...$lines );

echo "Also FILE_IGNORE_NEW_LINES:\n";
$lines2 = file($filename, FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES);
var_dump( ...$lines2 );
unlink($filename);

Resulted in this output:

Just FILE_SKIP_EMPTY_LINES:
string(6) "First
"
string(1) "
"
string(7) "Second
"
Also FILE_IGNORE_NEW_LINES:
string(5) "First"
string(6) "Second"

But I expected this output instead:

Just FILE_SKIP_EMPTY_LINES:
string(6) "First
"
string(7) "Second
"
Also FILE_IGNORE_NEW_LINES:
string(5) "First"
string(6) "Second"

PHP Version

8.3+

Operating System

No response

@DanielEScherzer
Copy link
Contributor Author

I'm happy to work on this, just wanted to confirm that this was indeed a bug rather than a documentation issue, since you can consider the newline to make the line non-empty

@nielsdos
Copy link
Member

Hm indeed the branch here (

if (include_new_line) {
) doesn't take that flag into account, while the else branch does.
Weird, to say the least.
You may want to try to dig through the ML history or commit history to see if there's a reason for this, but from a quick look I don't see a reasoning in the commit history. Didn't check the ML history.
The fix should target master though because it's a behaviour change (and should get a note in UPGRADING)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants