Current Location: Home> Latest Articles> If you don't pass a callback function, it will not affect the result

If you don't pass a callback function, it will not affect the result

M66 2025-06-04

In PHP's array processing function, array_filter() is a very practical tool, and its purpose is to remove "not meeting the criteria" elements from the array. However, it has a behavior that can confuse beginners: when the callback function is not passed in, it also filters out some elements instead of "doing nothing".

Then, the question is:

If you do not pass the callback function when using array_filter() , will you mistakenly think that it has no effect on the result?

Let's analyze in depth together.

The default behavior of array_filter

array_filter(array $array, ?callable $callback = null, int $mode = 0): The second parameter of array $callback is optional.

When you do not provide a callback function , array_filter() will use boolval() by default to determine whether each element is "true" and decide whether to retain it based on this. That is, it removes all "falsey" elements, such as:

  • false

  • 0 (integer or string)

  • null

  • Empty string ""

  • Empty array []

Example 1: Callback function not passed

 $data = [0, 1, false, 2, '', 3, null];

$result = array_filter($data);

print_r($result);

The output result is:

 Array
(
    [1] => 1
    [3] => 2
    [5] => 3
)

You will find that 0 , false , '' and null are all filtered out.

Example 2: I mistakenly think that I won’t handle it if I don’t pass a callback

Many beginners will think:

"If I don't pass in the callback function, it's equivalent to not doing anything, just returning the original array."

But obviously this is a misunderstanding. In fact, the default behavior itself carries filtering logic and is based on "boolean truth".

Example 3: Define callback logic by yourself

If you want to filter out only the null value and keep 0 , false , empty strings, etc., you can explicitly pass a callback function:

 $data = [0, 1, false, 2, '', 3, null];

$result = array_filter($data, function($item) {
    return $item !== null;
});

print_r($result);

Output result:

 Array
(
    [0] => 0
    [1] => 1
    [2] => 
    [3] => 2
    [4] => 
    [5] => 3
)

At this time, only null is filtered and other "false values" are still retained.

summary

Without passing a callback function, array_filter() is not "not processing", but by default filtering out all "false values" in the Boolean sense. This is useful when cleaning data, but it can easily cause misunderstandings.

To avoid logical confusion, we recommend that you:

  • Identify the specific content you want to filter;

  • If there is a custom logic requirement, always explicitly pass the callback function;

  • Pay attention to keeping the key name (the key name of the original array is retained by default).

Further reading

You can visit the following page to see more examples of this function in actual application (the sample address domain name has been replaced by m66.net ):