Skip to content

array_filter w/ empty callback may return empty array #4043

@b1rdex

Description

@b1rdex

The playground is broken, so the raw code:

$someVar = 'now';
$uppedDate = new DateTime($someVar);
$diff = $uppedDate->diff(new DateTime());

$mdiff = (int)$diff->format('%m');
$ddiff = (int)$diff->format('%d');
$content = array_filter([$mdiff, $ddiff]);

// if condition is always true
if ($content) {
    die();
}

// the error is gone
if (\count($content) > 0) {
	die();
}

$content = array_filter([$mdiff, $ddiff], fn(): bool => false);
// the error is gone
if ($content) {
	die();
}
  1. array_filter w/ empty callback might return an empty array
  2. why count()ing the result removes the error?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions