PHP array_all() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_all() checks whether every item in an array passes a test that you write as a callback function. It returns true only when the callback returns true for all items.

You use array_all() for questions like 'are all numbers even?' or 'did every field pass validation?'. It stops at the first item that fails, so it can be quick on large arrays. The callback receives the value first and the key second.

Syntax

Call array_all() with the values you need. Start with this line.

PHP
$allEven = array_all($numbers, fn($n) => $n % 2 === 0);
CallWhat it does
array_all([2, 4, 6], fn($n) => $n % 2 === 0)Returns true because every number is even.
array_all([2, 4, 5], fn($n) => $n % 2 === 0)Returns false because 5 is not even.
array_all([], fn($n) => $n > 0)Returns true because an empty array has no failing item.

array_all() was added in PHP 8.4. On older versions you can get the same result with a foreach loop, or by checking that array_filter() keeps the same number of items. The callback only needs to return something that is true or false.

Library declarations
PHP
array_all(array $array, callable $callback): bool

Parameters

ParameterDescription
arrayRequired. The array to check.
callbackRequired. A function that receives the value and the key, like fn($value, $key). It should return true when the item passes the test.

Return Value

Return ValueDescription
trueThe callback returned true for every item, or the array is empty.
falseThe callback returned false for at least one item.

PHP Examples

Example 1 — Testing Every Number

Pass an array and a callback. The result is a single true or false.

PHP
<?php
$even = [2, 4, 6, 8];
$mixed = [2, 4, 5];

var_dump(array_all($even, fn($n) => $n % 2 === 0));
var_dump(array_all($mixed, fn($n) => $n % 2 === 0));

How It Works

  1. The callback fn($n) => $n % 2 === 0 is true for even numbers.
  2. Every item in $even is even, so the first call returns true.
  3. In $mixed the value 5 is odd, so the callback returns false for it.
  4. One failing item is enough, so the second call returns false.

Example 2 — Empty Arrays and Using the Key

An empty array always gives true. The callback can also take the key as its second argument.

PHP
<?php
var_dump(array_all([], fn($n) => $n > 100));

$prices = ['apple' => 3, 'pear' => 5];
var_dump(array_all($prices, fn($price, $name) => $price >= 3 && strlen($name) >= 4));

How It Works

  1. The empty array has no item that could fail the test, so the first result is true.
  2. For the second array the callback receives the price and then the name.
  3. 'apple' has price 3 and 5 letters, and 'pear' has price 5 and 4 letters.
  4. Both items pass both conditions, so array_all() returns true.

Example 3 — Stopping at the First Failure

array_all() stops as soon as one item fails. A counter shows how many items were really checked.

PHP
<?php
$checked = 0;

$result = array_all([1, 2, 3, 4], function ($n) use (&$checked) {
    $checked++;
    return $n < 3;
});

var_dump($result);
echo $checked, PHP_EOL;

How It Works

  1. The callback adds 1 to $checked every time it runs.
  2. The values 1 and 2 pass the test because they are less than 3.
  3. The value 3 fails, so array_all() stops and never looks at 4.
  4. The result is false and the callback ran exactly 3 times.

Frequently Asked Questions

It returns true when a callback returns true for every item in the array, and false as soon as one item fails. It is a short way to ask whether all items match a rule.
array_all() was added in PHP 8.4. On PHP 8.3 and older it does not exist, so you need a foreach loop or a polyfill instead.
It returns true. There is no item that fails the test, so the condition holds for all zero items. Add an explicit empty check if you need a different result.
The callback receives the value first and the key second. You can write fn($value) and ignore the key, or fn($value, $key) when you need both.
array_all() needs every item to pass the test, while array_any() needs only one item to pass. For an empty array array_all() gives true and array_any() gives false.

Next: array_any()

array_any() checks whether at least one item in an array passes a test that you write as a.

array_any() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful