Skip to content

Instantly share code, notes, and snippets.

@xmsi
Last active February 21, 2025 06:46
Show Gist options
  • Save xmsi/64c10d03adc295b24bf0f64782580f97 to your computer and use it in GitHub Desktop.
Save xmsi/64c10d03adc295b24bf0f64782580f97 to your computer and use it in GitHub Desktop.
Packages Versioning in composer.json, PHP

There is a difference between ~ and ^ in Composer, though it might not be immediately obvious in some cases. Let me clarify:

Key Difference Between ~ and ^:

  • ~ (tilde): Allows updates for the last digit specified.
  • ^ (caret): Allows updates for all minor versions within the same major version.

In your example, ~7.3 vs. ^7.3:

  • ~7.3:

    • Means >=7.3.0 and <8.0.0 (allows updates to 7.3.x, 7.4.x, 7.5.x, etc.).
    • It happens to behave the same as ^7.3 in this case because both allow updates up to <8.0.0.
  • ^7.3:

    • Means >=7.3.0 and <8.0.0 (same range as ~7.3 here).

When They Differ:

The difference is clearer with more specific version constraints:

  1. Example: ~7.3.1 vs. ^7.3.1

    • ~7.3.1: >=7.3.1 and <7.4.0 (restricts updates to 7.3.x only).
    • ^7.3.1: >=7.3.1 and <8.0.0 (allows updates to 7.4.x, 7.5.x, etc.).
  2. Example: ~7.0 vs. ^7.0

    • ~7.0: >=7.0.0 and <8.0.0.
    • ^7.0: >=7.0.0 and <8.0.0.
    • In this case, they behave the same again.

TL;DR:

  • For ~7.3 and ^7.3, there is no difference because both allow updates from 7.3.0 up to <8.0.0.
  • However, ~ is stricter when you specify patch versions (e.g., ~7.3.1 allows less than ^7.3.1).

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment