PHPackages                             diff/diff - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. diff/diff

ActiveLibrary

diff/diff
=========

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

3.4.0(1y ago)2041.6M↑13.7%16[1 issues](https://github.com/wmde/Diff/issues)[5 PRs](https://github.com/wmde/Diff/pulls)11BSD-3-ClausePHPPHP &gt;=7.4CI passing

Since Jan 8Pushed 1y ago35 watchersCompare

[ Source](https://github.com/wmde/Diff)[ Packagist](https://packagist.org/packages/diff/diff)[ Docs](https://github.com/wmde/Diff)[ RSS](/packages/diff-diff/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (25)Used By (11)

Diff
====

[](#diff)

[![Build Status](https://github.com/wmde/Diff/actions/workflows/push.yaml/badge.svg?branch=master)](https://github.com/wmde/Diff/actions/workflows/push.yaml)[![Code Coverage](https://camo.githubusercontent.com/614f9b1eac328c117ca07bc204bc99686266a20ebf4bfb6f82f3e95b4aecca0f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f776d64652f446966662f6261646765732f636f7665726167652e706e673f733d36656636613734613932623765666336653236343730626232303932393331323566373037333165)](https://scrutinizer-ci.com/g/wmde/Diff/)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/c95a1f929452df270bc0fe261f24f4f21708c386d2e19536f499ae11a84f6f42/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f776d64652f446966662f6261646765732f7175616c6974792d73636f72652e706e673f733d64373564383736323437353934626234303838313539353734636564663762643634386239646232)](https://scrutinizer-ci.com/g/wmde/Diff/)[![Packagist Version](https://camo.githubusercontent.com/cdc7282a23adf91206b4bce963b80cbf2a7fa2dbcd79692b07f13c002cf1ed62/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646966662f646966663f6c6162656c3d56657273696f6e)](https://packagist.org/packages/diff/diff)[![Packagist Downloads](https://camo.githubusercontent.com/99880a4deeff84533f938add93c0c762ca88717080c4facab4b67e73b5515577/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646966662f646966663f6c6162656c3d446f776e6c6f616473)](https://packagist.org/packages/diff/diff)

**Diff** is a small standalone PHP library for representing differences between data structures, computing such differences, and applying them as patches. It is extremely well tested and allows users to define their own comparison strategies.

Diff does not provide any support for computing or representing the differences between unstructured data, ie text.

A full history of the different versions of Diff can be found in the [release notes](RELEASE-NOTES.md).

Requirements
------------

[](#requirements)

**Diff 3.x:**

- PHP 7.2 or later (tested with PHP 7.4 up to PHP 8.4)

**Diff 2.x:**

- PHP 5.3 or later (tested with PHP 5.3 up to PHP 7.1 and HHVM)

Installation
------------

[](#installation)

To add this package as a local, per-project dependency to your project, simply add a dependency on `diff/diff` to your project's [`composer.json`](https://getcomposer.org/) file. Here is a minimal example of a `composer.json` file that just defines a dependency on Diff 3.x:

```
{
    "require": {
        "diff/diff": "~3.0"
    }
}
```

High level structure
--------------------

[](#high-level-structure)

The Diff library can be subdivided into several components. The main components are:

- **DiffOp** Value objects that represent add, change, remove and composite operations.
- **Differ** Service objects to create a diff between two sets of data.
- **Patcher** Service objects to apply a diff as patch to a set of data.

There are two support components, which are nevertheless package public:

- **Comparer** Service objects for determining if two values are equal.
- **ArrayComparer** Service objects for computing the difference between to arrays.

Usage
-----

[](#usage)

### Representing diffs

[](#representing-diffs)

A diff consists out of diff operations. These can be atomic operations such as add, change and remove. These can also be diffs themselves, when dealing with nested structures. Hence the [composite pattern](https://en.wikipedia.org/wiki/Composite_pattern) is used.

Diff operations implement the **DiffOp** interface.

The available operations are:

- `DiffOpAdd` - addition of a value (newValue)
- `DiffOpChange` - modification of a value (oldValue, newValue)
- `DiffOpRemove` - removal of a value (oldValue)
- `Diff` - a collection of diff operations

These can all be found in [src/DiffOp](src/DiffOp).

The `Diff` class can be set to be either associative or non-associative. In case of the later, only `DiffOpAdd` and `DiffOpRemove` are allowed in it.

### Diffing data

[](#diffing-data)

To compute the difference between two data structures, an instance of **Differ** is used. The `Differ` interface has a single method.

```
/**
 * Takes two arrays, computes the diff, and returns this diff as an array of DiffOp.
 *
 * @param array $oldValues The first array
 * @param array $newValues The second array
 *
 * @throws Exception
 * @return DiffOp[]
 */
public function doDiff( array $oldValues, array $newValues ): array;
```

Implementations provided by Diff:

- `ListDiffer`: Differ that only looks at the values of the arrays (and thus ignores key differences).
- `MapDiffer`: Differ that does an associative diff between two arrays, with the option to do this recursively.
- `CallbackListDiffer`: Differ that only looks at the values of the arrays and compares them with a callback.
- `OrderedListDiffer`: Differ that looks at the order of the values and the values of the arrays.

All differ functionality can be found in [src/Differ](src/Differ).

### Applying patches

[](#applying-patches)

To apply a diff as a patch onto a data structure, an instance of **Patcher** is used. The `Patcher` interface has a single method.

```
/**
 * Applies the applicable operations from the provided diff to
 * the provided base value.
 *
 * @param array $base
 * @param Diff $diffOps
 *
 * @return array
 */
public function patch( array $base, Diff $diffOps ): array;
```

Implementations provided by Diff:

- `ListPatcher`: Applies non-associative diffs to a base. With default options does the reverse of `ListDiffer`
- `MapPatcher`: Applies diff to a base, recursively if needed. With default options does the reverse of `MapDiffer`

All classes part of the patcher component can be found in [src/Patcher](src/Patcher)

### ValueComparer

[](#valuecomparer)

The `ValueComparer` interface contains one method:

```
/**
 * @param mixed $firstValue
 * @param mixed $secondValue
 *
 * @return bool
 */
public function valuesAreEqual( $firstValue, $secondValue ): bool;
```

Implementations provided by Diff:

- `StrictComparer`: Value comparer that uses PHPs native strict equality check (ie ===).
- `CallbackComparer`: Adapter around a comparison callback that implements the `ValueComparer` interface.
- `ComparableComparer`: Value comparer for objects that provide an equals method taking a single argument.

All classes part of the ValueComparer component can be found in [src/Comparer](src/Comparer)

### ArrayComparer

[](#arraycomparer)

The `ArrayComposer` interface contains one method:

```
/**
 * Returns an array containing all the entries from arrayOne that are not present
 * in arrayTwo.
 *
 * Implementations are allowed to hold quantity into account or to disregard it.
 *
 * @param array $firstArray
 * @param array $secondArray
 *
 * @return array
 */
public function diffArrays( array $firstArray, array $secondArray ): array;
```

Implementations provided by Diff:

- `NativeArrayComparer`: Adapter for PHPs native array\_diff method.
- `StrategicArrayComparer`: Computes the difference between two arrays by comparing elements with a `ValueComparer`.
- `StrictArrayComparer`: Does strict comparison of values and holds quantity into account.
- `OrderedArrayComparer`: Computes the difference between two ordered arrays by comparing elements with a `ValueComparer`.

All classes part of the ArrayComparer component can be found in [src/ArrayComparer](src/ArrayComparer)

Examples
--------

[](#examples)

### Manually constructing a diff

[](#manually-constructing-a-diff)

```
$diff = new Diff( array(
	'email' => new DiffOpAdd( 'nyan@c.at' ),
	'awesome' => new DiffOpChange( 42, 9001 ),
) );
```

### Computing a diff

[](#computing-a-diff)

```
$oldVersion = array(
	'awesome' => 42,
);

$newVersion = array(
	'email' => 'nyan@c.at',
	'awesome' => 9001,
);

$differ = new MapDiffer();
$diff = $differ->doDiff( $oldVersion, $newVersion );
```

### Applying a diff as patch

[](#applying-a-diff-as-patch)

```
$oldVersion = array(
	/* ... */
);

$diff = new Diff( /* ... */ );

$patcher = new MapPatcher();
$newVersion = $patcher->patch( $oldVersion, $diff );
```

Links
-----

[](#links)

- [Diff on Packagist](https://packagist.org/packages/diff/diff)
- [Diff on OpenHub](https://www.openhub.net/p/phpdiff)
- [Diff on ScrutinizerCI](https://scrutinizer-ci.com/g/wmde/Diff/)

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance41

Moderate activity, may be stable

Popularity57

Moderate usage in the ecosystem

Community41

Growing community involvement

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 61.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~243 days

Recently: every ~614 days

Total

19

Last Release

497d ago

Major Versions

0.9 → 1.02014-04-10

1.0.1 → 2.0.02015-03-17

2.1.0 → 3.0.02017-05-10

2.3.0 → 3.1.02018-04-17

2.x-dev → 3.2.02018-09-11

PHP version history (4 changes)0.4PHP &gt;=5.3.0

3.0.0PHP &gt;=7.0

3.3.0PHP &gt;=7.2

3.4.0PHP &gt;=7.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/451bd4039d530fed8f9c3da91bfa519233a397d2182cdfdcad700f6cfea19b7f?d=identicon)[Jeroen De Dauw](/maintainers/Jeroen%20De%20Dauw)

![](https://www.gravatar.com/avatar/054adb441e7ee248ec924bc45fa793835c284710eb31627587fa5de21bab9e96?d=identicon)[wmde](/maintainers/wmde)

![](https://www.gravatar.com/avatar/5406ed1d40d50ffc61d67e9f5149914dbfe0b8a52bdf297299f5ccfab0a73d91?d=identicon)[thiemowmde](/maintainers/thiemowmde)

![](https://www.gravatar.com/avatar/fe4220e5a109212ea1a84969fbcb0795ceaf975145c1f0577bd758d0500e6428?d=identicon)[manicki](/maintainers/manicki)

![](https://www.gravatar.com/avatar/8cc7ef1a982b9520bbe19e7699556cda12f528e2fcece72d0d18e79b6457794c?d=identicon)[mariushoch](/maintainers/mariushoch)

---

Top Contributors

[![JeroenDeDauw](https://avatars.githubusercontent.com/u/146040?v=4)](https://github.com/JeroenDeDauw "JeroenDeDauw (358 commits)")[![translatewiki](https://avatars.githubusercontent.com/u/24829418?v=4)](https://github.com/translatewiki "translatewiki (65 commits)")[![thiemowmde](https://avatars.githubusercontent.com/u/6576639?v=4)](https://github.com/thiemowmde "thiemowmde (46 commits)")[![mariushoch](https://avatars.githubusercontent.com/u/2446964?v=4)](https://github.com/mariushoch "mariushoch (35 commits)")[![manicki](https://avatars.githubusercontent.com/u/3524114?v=4)](https://github.com/manicki "manicki (24 commits)")[![addshore](https://avatars.githubusercontent.com/u/3308769?v=4)](https://github.com/addshore "addshore (9 commits)")[![reedy](https://avatars.githubusercontent.com/u/67615?v=4)](https://github.com/reedy "reedy (8 commits)")[![DanweDE](https://avatars.githubusercontent.com/u/101926?v=4)](https://github.com/DanweDE "DanweDE (6 commits)")[![codders](https://avatars.githubusercontent.com/u/17782?v=4)](https://github.com/codders "codders (5 commits)")[![tobijat](https://avatars.githubusercontent.com/u/2997252?v=4)](https://github.com/tobijat "tobijat (5 commits)")[![makstech](https://avatars.githubusercontent.com/u/25028046?v=4)](https://github.com/makstech "makstech (4 commits)")[![siebrand](https://avatars.githubusercontent.com/u/210297?v=4)](https://github.com/siebrand "siebrand (4 commits)")[![cordoval](https://avatars.githubusercontent.com/u/328359?v=4)](https://github.com/cordoval "cordoval (2 commits)")[![Ladsgroup](https://avatars.githubusercontent.com/u/5351225?v=4)](https://github.com/Ladsgroup "Ladsgroup (2 commits)")[![marko-ilic](https://avatars.githubusercontent.com/u/25265308?v=4)](https://github.com/marko-ilic "marko-ilic (1 commits)")[![hashar](https://avatars.githubusercontent.com/u/281689?v=4)](https://github.com/hashar "hashar (1 commits)")[![johl](https://avatars.githubusercontent.com/u/35403?v=4)](https://github.com/johl "johl (1 commits)")[![filbertkm](https://avatars.githubusercontent.com/u/135401?v=4)](https://github.com/filbertkm "filbertkm (1 commits)")[![JanZerebecki](https://avatars.githubusercontent.com/u/7452727?v=4)](https://github.com/JanZerebecki "JanZerebecki (1 commits)")[![mlazowik](https://avatars.githubusercontent.com/u/1978721?v=4)](https://github.com/mlazowik "mlazowik (1 commits)")

---

Tags

diffpatchingwikidatapatchdiffingdiffop

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/diff-diff/health.svg)

```
[![Health](https://phpackages.com/badges/diff-diff/health.svg)](https://phpackages.com/packages/diff-diff)
```

###  Alternatives

[sebastian/diff

Diff implementation

7.7k901.5M250](/packages/sebastian-diff)[yetanotherape/diff-match-patch

Port of the google-diff-match-patch (https://github.com/google/diff-match-patch) lib to PHP

158915.9k10](/packages/yetanotherape-diff-match-patch)[jfcherng/php-diff

A comprehensive library for generating differences between two strings in multiple formats (unified, side by side HTML etc).

4705.1M51](/packages/jfcherng-php-diff)[caxy/php-htmldiff

A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.

21520.9M15](/packages/caxy-php-htmldiff)[vaimo/composer-patches

Applies a patch from a local or remote file to any package that is part of a given composer project. Patches can be defined both on project and on package level. Optional support for patch versioning, sequencing, custom patch applier configuration and patch command for testing/troubleshooting added patches.

2994.3M16](/packages/vaimo-composer-patches)[localheinz/diff

Fork of sebastian/diff for use with ergebnis/composer-normalize

4637.0M5](/packages/localheinz-diff)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
