Current Location: Home> Latest Articles> Don't understand the Boolean meaning of the callback function return value

Don't understand the Boolean meaning of the callback function return value

M66 2025-06-06

In PHP, array_filter() is a very common array function that is used to filter elements in an array based on the result of the callback function. Its usage may seem simple, but many developers are puzzled by the "boolean meaning" of their return value when using it, especially when writing callback functions.

For example, someone wrote the following code:

 $input = [0, 1, 2, 3, 4, 5];

$result = array_filter($input, function($value) {
    return $value % 2; // Hope to keep odd numbers
});

print_r($result);

They might expect the output to be [1, 3, 5] , and so does the result. However, if you make a little modification to this example, it will easily cause confusion:

 $input = [0, 1, 2, 3, 4, 5];

$result = array_filter($input, function($value) {
    return $value % 2 == 0; // Hope to keep even numbers
});

print_r($result);

Many people think that if the elements are retained when returning true , and if they return false , they are filtered out. This is indeed true. But the key is that - PHP is a dynamically typed language, and any value returned by the callback function will be cast to a Boolean value.

The criteria for judging "true and false"

These values ​​in PHP are converted to false :

  • Boolean value false

  • Integer 0

  • Floating point type 0.0

  • Empty string '' or '0'

  • Empty array []

  • null

So, when you write the following code:

 $input = ['a', '', 'b', '0', 'c'];

$result = array_filter($input);

print_r($result);

You will find that the output is:

 Array
(
    [0] => a
    [2] => b
    [4] => c
)

When no callback function is specified, array_filter() uses a Boolean cast by default to determine whether an element is retained. So both empty strings '' and '0' are filtered out - because they are converted to false .

At this time, many people will be confused: "I clearly have value, why has it been filtered out?"

The trap of the callback function's return value

Further misunderstandings usually occur when developers use callbacks to return non-boolean types:

 $input = [1, 2, 3, 4, 5, 6];

$result = array_filter($input, function($value) {
    return $value & 1; // Use bitwise and judge odd numbers
});

print_r($result);

This code is correct, but it may drive beginners crazy. The result of $value & 1 is 0 or 1. When $value is an even number, the result is 0 , which will be converted to false and is filtered out. At this time, you must understand that it is not that the value returning 1 or 0 itself determines whether the elements are retained, but the performance of these values ​​in a Boolean environment.

A detail that is easy to ignore: key names are retained

Another thing that many people have not noticed: array_filter() retains the key name of the original array. If you want to rebuild the index, you can use array_values() :

 $result = array_values(array_filter($input, function($value) {
    return $value > 2;
}));

Practical case: Filter out the situation where there are no specified parameters in the URL

For example, you want to filter out links without token parameters from a set of URLs:

 $urls = [
    'https://m66.net/page1?token=abc',
    'https://m66.net/page2',
    'https://m66.net/page3?token=xyz',
];

$filtered = array_filter($urls, function($url) {
    $parts = parse_url($url);
    if (!isset($parts['query'])) {
        return false;
    }
    parse_str($parts['query'], $query);
    return isset($query['token']);
});

print_r($filtered);

The callback function of this code explicitly returns a Boolean value ( true or false ) so that it will not be confused by type conversion.

Summarize

Many people do not understand the "boolean meaning" of the return value of the array_filter() callback function. In the final analysis, they do not have a deep understanding of the "true and false values" in PHP. To write a reliable array_filter() callback function, remember:

  • The return value of the callback function is converted to a Boolean value.

  • Do not directly depend on whether a type (such as a number or string) has a "value", but explicitly return true or false .

  • If your intention is clear logical judgment, write the Boolean expression explicitly.

In this way, you can completely master this powerful and easily misunderstood function!