I’ve been noticing people having trouble understanding the differences between the caret (“^”) and the tilde (“~”) operator in the composer.json file. Composer’s documentation is great but a bit short, that is why I write this blog post.
Composer assumes that all packages are using Semver. It is an easy version schema that consists of a major.minor.patch version. A major version breaks backwards compatibility, minor version introduces new features and patch versions are bug fixes.
The tilde operator is best explained by example. Notice that it actually makes a difference if you specify the patch version or not.
The caret operator follows Semver.
What is little less known is that Semver behaves differently on versions under 1.0.0. Before the project has a stable release, minor versions are allowed to break backwards compatibility. This is dangerous when you use the tilde operator since you may get update that breaks backwards compatibility.
A workaround for this could be that you always specify versions under 1.0.0 with an asterisk (“*”).
A better approach is to continue to use the caret operator as it still respects Semver even on versions under 1.0.0.
In conclusion: One should always use caret (“^”) over tilde (“~”) because caret is always respecting Semver.