last modified March 12, 2025
In this article, we explore predicates in PHP. A predicate is a function that returns a boolean value, typically used to test conditions or validate data. Predicates are useful for filtering, validating, or making decisions in code.
A predicate is a function that takes one or more arguments and returns true or false. Predicates are often used in functional programming to filter arrays, validate inputs, or make decisions.
The following example demonstrates a simple predicate that checks if a number is even.
main.php
<?php
declare(strict_types=1);
function isEven(int $number): bool { return $number % 2 === 0; }
$result = isEven(4); echo $result ? ’true’ : ‘false’;
In this program, the isEven function is a predicate that checks if a number is even. It returns true if the number is even and false otherwise.
$ php main.php true
The following example demonstrates how to use a predicate to filter an array.
main.php
<?php
declare(strict_types=1);
function isPositive(int $number): bool { return $number > 0; }
$numbers = [-1, 2, -3, 4, -5]; $positiveNumbers = array_filter($numbers, ‘isPositive’);
print_r($positiveNumbers);
In this program, the isPositive predicate filters out negative numbers from the array. The array_filter function applies the predicate to each element of the array.
$ php main.php Array ( [1] => 2 [3] => 4 )
The following example demonstrates how to use an anonymous function as a predicate.
main.php
<?php
declare(strict_types=1);
$numbers = [1, 2, 3, 4, 5]; $evenNumbers = array_filter($numbers, function(int $number): bool { return $number % 2 === 0; });
print_r($evenNumbers);
In this program, an anonymous function is used as a predicate to filter even numbers from the array. The array_filter function applies the predicate to each element of the array.
$ php main.php Array ( [1] => 2 [3] => 4 )
The following example demonstrates a predicate with multiple conditions.
main.php
<?php
declare(strict_types=1);
function isValidEmail(string $email): bool { return filter_var($email, FILTER_VALIDATE_EMAIL) !== false; }
$emails = [’test@example.com’, ‘invalid-email’, ‘user@domain.com’]; $validEmails = array_filter($emails, ‘isValidEmail’);
print_r($validEmails);
In this program, the isValidEmail predicate checks if an email is valid using PHP’s filter_var function. The array_filter function applies the predicate to each element of the array.
$ php main.php Array ( [0] => test@example.com [2] => user@domain.com )
The following example demonstrates a predicate that validates an object.
main.php
<?php
declare(strict_types=1);
class User { public function __construct(public int $age) {} }
function isAdult(User $user): bool { return $user->age >= 18; }
$users = [new User(15), new User(20), new User(17)]; $adults = array_filter($users, ‘isAdult’);
print_r($adults);
In this program, the isAdult predicate checks if a user is an adult based on their age. The array_filter function applies the predicate to each element of the array.
$ php main.php Array ( [1] => User Object ( [age] => 20 ) )
The following example demonstrates a predicate with strict type declaration.
main.php
<?php
declare(strict_types=1);
function isStringLong(string $text): bool { return strlen($text) > 10; }
$texts = [‘short’, ‘a longer text’, ‘another long text’]; $longTexts = array_filter($texts, ‘isStringLong’);
print_r($longTexts);
In this program, the isStringLong predicate checks if a string is longer than 10 characters. The array_filter function applies the predicate to each element of the array.
$ php main.php Array ( [1] => a longer text [2] => another long text )
PHP Anonymous Functions - Documentation
In this article, we have shown how to use predicates in PHP for filtering, validation, and decision-making. Predicates are a powerful tool for writing clean and reusable code.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP tutorials.