Home Development Tools composer What is the difference between Composer's tilde symbol ~ and the power symbol ^

What is the difference between Composer's tilde symbol ~ and the power symbol ^

Aug 12, 2020 pm 01:16 PM
composer

The following tutorial column of composer will introduce to you the difference between Composer’s tilde symbol ~ and the power symbol ^. I hope it will be helpful to friends in need!

What is the difference between Composer's tilde symbol ~ and the power symbol ^

I have seen too many people writing version numbers randomly (in fact, it is much better now than before). The next version of 1.9.0 must be 2.0.0, 2.9 .0 The next version must be 3.0.0... Mainly because there was no standard naming of version numbers before, and everyone wrote according to their mood. Later, semantic versioning was promoted, and the situation of version number naming was improved, but there are still many people who don’t know what the three-segment version number means (will you die if you teach these specifications in various training schools...).

To put it simply (I am just moving it, the website mentioned above makes it clearer), the meaning of each segment of the three-segment version number is:

Large version. Small version. Patch version

Let’s talk about the most obvious patch version first. If your software or library has had some bugs fixed before, the patch version needs to be 1.

Let’s talk about the minor version number. If you add a new feature, such as an RPG game that automatically picks up things, or a new method in your code base, it will not cause compatibility issues (BC) if users upgrade your code base, so a small version No. 1.

Finally, if your software is completely refactored, or your code base is completely refactored so that users upgrading the new library will inevitably cause compatibility issues, then the major version number is 1.

Of course, there will be cases where bugs are modified and functions are added at the same time. In short, if the previous bit requires 1, the following bits must be cleared to zero in any case.

In fact, there is no need to be too strict about software for end users, because end users don’t care much about the version (except for the big version number..., so chrome-style version numbers appeared, and Firefox also followed suit. ...), but for library file authors, it is better to abide by the version number, because the version number you set provides very important information to library callers, and they need to use your version number to determine whether to avoid compatibility issues. .

To give a more classic example, although Symfony 2/3/4 are similar in use, because there are interface inconsistencies between these three versions, the major version number must be 1. If your library file has an interface change that makes it incompatible with previous versions, even if there is only one, you should change the major version number to 1.

And if you develop some projects using Symfony 3.0.x, even if you upgrade directly to 3.4.x, theoretically there is no problem at all.

After talking about the three-segment version number, let’s talk about a problem that troubles PHPer. What do the ^ symbols and ~ symbols that specify the version number in composer.json mean? What's the difference?

Before writing this blog, I actually searched on Baidu and wanted to know if there were many related articles and whether this article was worth writing. The search results show that someone has indeed summarized it. Although most of them only have one example:

~1.2.3 represents 1.2.3 <= version number< 1.3.0

^1.2.3 represents 1.2.3 <= version number< ; 2.0.0

Based on the above transfer content, I will add a little more, ~ means that the version number can only change the last paragraph (if it is ~x.y, the end is y, if it is ~x.y.z, the end is z), For example, in this case:

~1.2 is equal to >=1.2.0; <2.0.0

and the ^ symbol is related to the semantic versioning mentioned above. It means to try to use the latest version, but to ensure that no compatibility problems occur. In other words, in addition to the major version number, the minor version number and the patch version number can be changed.

But it seems that there is a very important detail that no one has mentioned yet. In semantic versioning, the version number starting with 0 is quite special, and because ^ strictly abides by the semantic versioning rules, the following will appear. Situation (earlier composer versions did not have this processing, but it was later corrected):

^0.3.0 is equal to>=0.3.0; <0.4.0

eh? Shouldn't it be <1.0.0?

The reason is that the rule of semantic versioning is that if the major version number starts with 0, it means that it is an unstable version (unstable). If it is in an unstable state, the minor version number is not allowed to be backward compatible! So if you want to specify a library starting with 0, you must pay attention:

~0.1 This way of writing is very dangerous, because ~0.1 is equal to >=0.1.0; <1.0.0, which may cause failure In the case of backward compatibility, the safer way to write it is

^0.1 (equal to >=0.1.0; <0.2.0)

The above is the detailed content of What is the difference between Composer's tilde symbol ~ and the power symbol ^. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

How to quickly build LaravelCMS with Composer: mki-labs/espresso practical experience How to quickly build LaravelCMS with Composer: mki-labs/espresso practical experience Apr 18, 2025 am 07:36 AM

I encountered a tricky problem when developing a new Laravel project: how to quickly build a fully functional and easy-to-manage content management system (CMS). I tried multiple solutions, but all gave up because of complex configuration and inconvenient maintenance. Until I discovered the LaravelCMS package mki-labs/espresso, which not only simple to install, but also provides powerful functions and intuitive management interface, which completely solved my problem.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Use Composer to solve dependency injection: application of PSR-11 container interface Use Composer to solve dependency injection: application of PSR-11 container interface Apr 18, 2025 am 07:39 AM

I encountered a common but tricky problem when developing a large PHP project: how to effectively manage and inject dependencies. Initially, I tried using global variables and manual injection, but this not only increased the complexity of the code, it also easily led to errors. Finally, I successfully solved this problem by using the PSR-11 container interface and with the power of Composer.

How to solve the problem of JavaScript error handling using Composer How to solve the problem of JavaScript error handling using Composer Apr 18, 2025 am 08:30 AM

I'm having a tough problem when developing a complex web application: how to effectively handle JavaScript errors and log them. I tried several methods, but none of them could meet my needs until I discovered the library dvasilenko/alterego_tools. I easily solved this problem through the installation of this library through Composer and greatly improved the maintainability and stability of the project. Composer can be learned through the following address: Learning address

How to use Composer to solve the problem of batch processing of data under Yii framework How to use Composer to solve the problem of batch processing of data under Yii framework Apr 18, 2025 am 07:54 AM

When developing Yii framework projects, you often encounter situations where you need to obtain a large amount of data from the database. If appropriate measures are not taken, directly obtaining all data may cause memory overflow and affect program performance. Recently, when I was dealing with a project on a large e-commerce platform, I encountered this problem. After some research and trial, I finally solved the problem through the extension library of pavle/yii-batch-result.

How to resolve HTTP request issues using Composer: A practical guide to the yiche/http library How to resolve HTTP request issues using Composer: A practical guide to the yiche/http library Apr 18, 2025 am 08:06 AM

During development, HTTP requests are often required, which may be to get data, send data, or interact with external APIs. However, when faced with complex network environments and changing request requirements, how to efficiently handle HTTP requests becomes a challenge. I have encountered a problem in a project: I need to send requests to different APIs frequently, and log the requests to facilitate subsequent debugging and analysis. After trying several methods, I discovered the yiche/http library. It not only simplifies the processing of HTTP requests, but also provides dynamic logging functions, greatly improving development efficiency.

How to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa library How to use Composer to improve the security of Laravel applications: Applications of wiebenieuwenhuis/laravel-2fa library Apr 18, 2025 am 11:36 AM

When developing a Laravel application, I encountered a common but difficult problem: how to improve the security of user accounts. With the increasing complexity of cyber attacks, a single password protection is no longer enough to ensure the security of users' data. I tried several methods, but the results were not satisfactory. Finally, I installed the wiebenieuwenhuis/laravel-2fa library through Composer and successfully added two-factor authentication (2FA) to my application, greatly improving security.

See all articles