Botanically, “spines” are distinguished from “thorns”: spines are modified leaves, and thorns are modified branches. Cacti produce spines, always from areoles as noted above. Spines are present even in those cacti with leaves, such as Pereskia, Pereskiopsis and Maihuenia, so they clearly evolved before complete leaflessness. Some cacti only have spines when young, possibly only when seedlings. This is particularly true of tree-living cacti, such as Rhipsalis and Schlumbergera, but also of some ground-living cacti, such as Ariocarpus.
The spines of cacti are often useful in identification, since they vary greatly between species in number, color, size, shape and hardness, as well as in whether all the spines produced by an areole are similar or whether they are of distinct kinds. Most spines are straight or at most slightly curved, and are described as hair-like, bristle-like, needle-like or awl-like, depending on their length and thickness.
Some cacti have flattened spines (e.g. Sclerocactus papyracanthus). Other cacti have hooked spines. Sometimes, one or more central spines are hooked, while outer spines are straight (e.g., Mammillaria rekoi). In addition to normal-length spines, members of the subfamily Opuntioideae have relatively short spines, called glochids, that are barbed along their length and easily shed. These enter the skin and are difficult to remove due to being very fine and easily broken, causing long-lasting irritation.
Spines, which are modified leaves, are present on even those cacti with true leaves, showing the evolution of spines preceded the loss of leaves. Spines provide protection from herbivores and camouflage in some species, and assist in water conservation in several ways. They trap air near the surface of the cactus, creating a moister layer that reduces evaporation and transpiration. They can provide some shade, which lowers the temperature of the surface of the cactus, also reducing water loss. When sufficiently moist air is present, such as during fog or early morning mist, spines can condense moisture, which then drips onto the ground and is absorbed by the roots.
Credit; https://en.wikipedia.org/wiki/Cactus