PHP Reading notes (3)-constants

Source: Internet
Author: User
<span id="Label3"></p><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">What is a constant</span></span><p class="simpara"><p class="simpara"><span style="font-family: Microsoft YaHei;"><span style="font-size: medium;"> <span style="font-size: medium;">What is a constant? Constants can be understood as values that do not change, or when a constant value is defined, it cannot be changed anywhere else in the script. This is how the manual defines PHP constants:</span> constants are identifiers (names) of simple Values. As the name implies, the value cannot be changed during script execution (except for the so-called <span class="link">magic constants, they are not constants). Changshime is considered to be case Sensitive. traditionally, constant identifiers are always capitalized. </span></span></span></p></p><span style="font-family: Microsoft YaHei; font-size: medium;"><span style="font-family: Microsoft YaHei; font-size: medium;">       </span></span><span style="font-family: Microsoft YaHei; font-size: medium;"><span style="font-family: Microsoft YaHei; font-size: medium;">the constant name and any other PHP tags follow the same naming conventions. A valid constant name begins with a letter or underscore followed by any letter, number, or underscore. This is expressed in regular expressions:<em>[a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*</em>. </span></span><span style="font-family: Microsoft YaHei; font-size: medium;"><span style="font-family: Microsoft YaHei; font-size: medium;"><br></span></span><p align="left"><p align="left"><span style="font-family: Microsoft YaHei;"><span style="font-size: 14px;"><span style="font-size: medium;"> <span style="font-size: medium;"> <span style="font-size: medium;">Constants in PHP are divided into custom constants and system constants</span> .</span></span><br></span></span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">A custom constant is a constant defined according to the needs of our development, and it is defined by using the function define () in Php.</span></p></p><p class="simpara"><p class="simpara">After PHP 5.3.0, You can use the <em>const</em> keyword to define constants outside of the class Definition. Once a constant is defined, it can no longer be changed or Undefined.</p></p><p class="simpara"><p class="simpara">Constants can only contain scalar data (<span class="type"><span class="type boolean">boolean,<span class="type"><span class="type integer">integer,<span class="type"><span class="type float">float, and <span class="type"><span class="type string">string). You can define <span class="type"><span class="type resource">resource constants, but avoid them as much as possible, because they cause unpredictable results. </span></span></span></span></span></span></span></span></span></span></p></p><p class="simpara"><p class="simpara">You can simply get the value of a constant by specifying its name, and unlike a variable, you <em class="emphasis">should not</em> precede the constant with the <em>$</em> symbol. If the constant name is dynamic, you can also use <span class="function"><span class="function">the function constant () to get the value of the Constant. <span class="function"><span class="function">A list of all defined constants can be obtained with get_defined_constants (). </span></span></span></span></p></p><span><span><span><span style="font-family: Microsoft YaHei; font-size: medium;">the second parameter, "value," is a required parameter, which is the value of the Constant. </span></span></span></span><p><p></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">The third parameter, "case_sensitive", is an optional parameter, specifying whether it is case sensitive, set to True to be insensitive, and generally without specifying a third parameter, the value of the default third parameter is False.</span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">(note: string indicates that the parameter type is a string type, mixed indicates that the parameter type can be accepted as a number of different types, Case_sensitive = True indicates that the default is Boolean type True)</span></p></p><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"><br></span></span><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"><strong>What is the use of constants? </strong></span></span><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">The main function of a constant is to avoid duplicate definitions and tamper with variable Values. When we are working on Team development, or when the code is very large, for some quantities that do not change after the first definition, if we use variables, without knowing the same variable name, the value of the variable will be replaced, which will cause the server to perform the wrong task.</span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">In addition, the use of constants can also improve the maintainability of the Code. If for some reason the value of the constant needs to be changed, we only need to modify one place. For example, in the calculation, at first we take Pi to 3.14, so many of the calculations we use 3.14 to calculate, when the need to increase the accuracy of the calculation, PI needs 3.142, We have to modify all the use of 3.14 code, if the code is more than a long time, not only a large amount of work, but also may be omitted.</span></p></p><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"> <span style="background-color: #ffcc99;"> <strong>Feel the benefits of constant! </strong><br></span></span></span><pre><pre><span style="color: #008080;">1</span> <? <span style="color: #000000;">PHP</span> <span style="color: #008080;">2</span> <span style="color: #008080;">Define</span> ("PI", 3.14<span style="color: #000000;">);</span> <span style="color: #008080;">3</span> <span style="color: #800080;">$r</span>=3<span style="color: #000000;">;</span> <span style="color: #008080;">4</span> <span style="color: #0000ff;">Echo</span> "area is:". (<span style="color: #008080;">PI</span>*<span style="color: #800080;">$r</span>*<span style="color: #800080;">$r</span>). " <br/> "<span style="color: #000000;">;</span> <span style="color: #008080;">5</span> <span style="color: #0000ff;">Echo</span> "perimeter is:". (the<span style="color: #008080;">PI</span>*<span style="color: #800080;">$r</span>). " <br/> "<span style="color: #000000;">;</span> <span style="color: #008080;">6</span> ?></pre></pre><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">Meet System Constants</span></span><p class="simpara"><p class="simpara"><span style="font-family: Microsoft YaHei; font-size: 14px;">PHP provides a large number of <span style="color: #3366ff;"> <span style="color: #3333ff;"><span class="link">predefined constants</span></span> to any script it runs <span style="color: #000000;">. </span></span> however, Many constants are defined by different extensions, and are only present when the extensions are loaded, either dynamically after loading, or they are included at compile Time.</span></p></p><p class="para"><p class="para"><span style="font-family: Microsoft YaHei; font-size: medium;">There are eight magic constants whose values change as they change position in the Code. For <span style="color: #cc0000;"><strong> <code>__LINE__</code> </strong></span> example, A value depends on the row it is in the script to Determine. These special constants are case-insensitive, as Follows:</span></p></p> <table class="doctable table xhe-border"> <caption> <span style="font-family: Microsoft YaHei; font-size: medium;"><strong>several "magic constants" for PHP</strong></span> </caption> <thead> <tr> <th><span style="font-family: Microsoft YaHei; font-size: medium;">name</span></th> <th> <span style="font-family: Microsoft YaHei; font-size: medium;">Description</span></th> </tr> </thead> <tbody class="tbody"> <tr id="constant.line"> <td><span style="color: #cc0000; font-family: Microsoft YaHei; font-size: medium;"><strong><code>__LINE__</code></strong></span></td> <td><span style="font-family: Microsoft YaHei; font-size: medium;">The current line number in the File.</span></td> </tr> <tr id="constant.file"> <td><span style="color: #cc0000; font-family: Microsoft YaHei; font-size: medium;"><strong><code>__FILE__</code></strong></span></td> <td><span style="font-family: Microsoft YaHei; font-size: medium;">The full path and file name of the File. If used in the included file, returns the file name that is Included. Since PHP 4.0.2,<strong> <code>__FILE__</code> </strong> always contains an absolute path (if the symbolic connection is the resolved absolute path), and the previous version sometimes contains a relative path.</span></td> </tr> <tr id="constant.dir"> <td><span style="color: #cc0000; font-family: Microsoft YaHei; font-size: medium;"><strong><code>__DIR__</code></strong></span></td> <td><span style="font-family: Microsoft YaHei; font-size: medium;">The directory where the file Resides. If used in the included file, returns the directory where the included files are Located. It is equivalent to <em>dirname (__file__)</em>. Unless it is a root directory, the name in the directory does not include the trailing Slash. (new in PHP 5.3.0) =</span></td> </tr> <tr id="constant.function"> <td><span style="color: #cc0000; font-family: Microsoft YaHei; font-size: medium;"><strong><code>__FUNCTION__</code></strong></span></td> <td><span style="font-family: Microsoft YaHei; font-size: medium;">The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is Defined. In PHP 4, This value is always in lowercase letters.</span></td> </tr> <tr id="constant.class"> <td><span style="color: #cc0000; font-family: Microsoft YaHei; font-size: medium;"><strong><code>__CLASS__</code></strong></span></td> <td><span style="font-family: Microsoft YaHei; font-size: medium;">The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive). In PHP 4, This value is always in lowercase letters. The class name includes its declared action area (for example, <em>foo\bar</em>). Note Since PHP 5.4, __class__ has also worked for Trait. When used in the trait method, __class__ is the name of the class that invokes the trait Method.</span></td> </tr> <tr id="constant.trait"> <td><span style="color: #cc0000; font-family: Microsoft YaHei; font-size: medium;"><strong><code>__TRAIT__</code></strong></span></td> <td><span style="font-family: Microsoft YaHei; font-size: medium;">Trait's name (PHP 5.4.0 new plus). From PHP 5.4 This constant returns the name of the trait when it is defined (case-sensitive). The Trait name includes its declared function area (for example, <em>foo\bar</em>).</span></td> </tr> <tr id="constant.method"> <td><span style="color: #cc0000; font-family: Microsoft YaHei; font-size: medium;"><strong><code>__METHOD__</code></strong></span></td> <td><span style="font-family: Microsoft YaHei; font-size: medium;">The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).</span></td> </tr> <tr id="constant.namespace"> <td><span style="color: #cc0000; font-family: Microsoft YaHei; font-size: medium;"><strong><code>__NAMESPACE__</code></strong></span></td> <td><span style="font-family: Microsoft YaHei; font-size: medium;">The name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 is new).</span></td> </tr> </tbody> </table><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"><br></span></span><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">how constants are evaluated</span></span><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">Constants are defined, then constants are used, so how do we get constant values?</span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">There are two ways to take a value for a constant Value.</span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">The first is to get the value directly using the constant name, for example to calculate the area of pi, as Follows:</span></p></p><pre><pre><span style="color: #008080;">1</span> <? <span style="color: #000000;">PHP</span> <span style="color: #008080;">2</span> <span style="color: #008080;">Define</span> ("PI", 3.14<span style="color: #000000;">);</span> <span style="color: #008080;">3</span> <span style="color: #800080;">$r</span>=1<span style="color: #000000;">;</span> <span style="color: #008080;">4</span> <span style="color: #800080;">$area</span> <span style="color: #008080;">PI</span>*<span style="color: #800080;">$r</span>*<span style="color: #800080;">$r</span><span style="color: #008000;">//</span> <span style="color: #008000;">Calculate the area of the Circle</span> <span style="color: #008080;">5</span> ?></pre></pre><p><p><span style="font-family: Microsoft YaHei;"><span style="font-size: medium;">The second is the use of the constant () Function. It is the same as the direct use of the output of the constant name, but the function can be dynamically output different constants, in the use of flexible, convenient, The syntax format is as Follows:</span></span></p></p><pre class="code"><pre class="code"><span style="color: #cc0000; font-family: Microsoft YaHei;"><span style="font-size: 14px;">Mixed constant (string Constant_name)</span></span></pre></pre>The first parameter, constant_name, is the name of the constant to get, or a variable that stores the constant Name. If successful, the value of the constant is returned, and failure indicates that the error message constant is not defined. (note: mixed indicates that the return value type of the function is of many different types, string indicates that the parameter type is a string Type)<span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;">How to determine if a constant is defined</span></span><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">If the constants are repeatedly defined, the PHP parser will issue a "Constant XXX already defined" warning to remind us that the constant has been defined. So how do we determine if a constant is defined when team development, or the amount of code is great?</span></p></p><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">The defined () function helps us to determine whether a constant has been defined and its syntax is:</span></p></p><pre class="code"><pre class="code"><span style="color: #cc0000; font-family: Microsoft YaHei; font-size: medium;">BOOL defined (string Constants_name)<span id="transmark" style="display: none; width: 0px; height: 0px;"></span></span></pre></pre><p align="left"><p align="left"><span style="font-family: Microsoft YaHei; font-size: medium;">It only has the parameter constant_name, refers to gets the name of the constant, returns the Boolean type True if present, otherwise returns the Boolean type false; (note: bool means the return value type of the function is a Boolean Type)</span></p></p><span style="font-family: Microsoft YaHei;"><span style="font-family: Microsoft YaHei;"><br></span></span><p><p>PHP Reading notes (3)-constants</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.