If we encounter a constant value that cannot be changed in actual programming, we usually define a constant to use this constant value. Then at this time we use
When defining a constant, we can use const to modify the constant. The constants modified with PHP const are slightly different from other constants: Do not use "$" before the constant name, remember! Of course, this constant value cannot be modified. Once defined, it cannot be "artificially" modified anywhere in the program. This is the same as using define to define, and using const to define of course also follows the naming rules of other constants - use large letters.
Let’s look at a small example of PHP const:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>class say_const{ </span></li><li class="alt"><span>const </span><span class="attribute">CHARSET</span><span>=</span><span class="attribute-value">"中国"</span><span>; </span></li><li><span>publice function say_hello(){ </span></li><li class="alt"><span>echo slef::CHARSET; </span></li><li><span>} </span></li><li class="alt"><span>} </span></li><li><span>$</span><span class="attribute">const1</span><span>=</span><span class="attribute-value">new</span><span> say_const()' </span></li><li class="alt"><span>$const1-</span><span class="tag">></span><span>say_hello(); </span></span></li> <li> <span class="tag">?></span><span> </span> </li> <li class="alt"><span> </span></li> </ol>
The above is an introduction to the specific usage of PHP const.