"PHP Manual" constants

Source: Internet
Author: User
Tags scalar

This article mainly introduces the constants in PHP, interested in the reference of friends, I hope to be helpful to everyone.

A constant is an identifier (first name) of a simple value. The range of constants is global. Changshime is considered to be case sensitive. Traditionally, constant identifiers are always uppercase.

Constant names follow the PHP tag naming conventions, starting with letters or underscores, followed by letters, numbers, or underscores. The regular expression is: [a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*

The letters refer to A-z,a-z, and ASCII characters from 127 to 255 (0X7F-0XFF).
Example #1 valid and illegal constant name <?php//valid constant name define ("FOO",     "something");d efine ("FOO2",    "Something Else");d efine (" Foo_bar "," something More ");//illegal constant name define (" 2FOO ",    " something ");//The following definitions are legal, but should be avoided: (Custom constants do not start with __)// Maybe someday php will define a __foo__ magic constant//This will conflict with your code define ("__foo__", "something"); >

Grammar

Define constants:

Define constants with the Define () function. Use the defined () function to check if a constant is defined. Use the const keyword to define constants outside the class definition (PHP 5.3.0).

Defining constants using the Const keyword must be at the top of the scope, because this method is defined at compile time. This means that constants cannot be defined within a function, within a loop, or within an if statement with a const.

Constants can only contain scalar data (boolean,integer,float and string). You can define resource constants, but they cause unpredictable results. The dynamic constant name, using the function constant () to get the constant value. A list of all defined constants can be obtained with get_defined_constants ().

Constants and variables have the following differences:

A constant is not preceded by a dollar sign ($); a constant can only be defined with the Define () function, not by an assignment statement; constants are defined and accessed anywhere, regardless of the scope of the variable; constants cannot be redefined or undefined once defined; The value of a constant can only be scalar. * Constants and (global) variables are in different namespaces. This means, for example, that TRUE and $TRUE are different.
Example #1 Define Constants <?php  define ("CONSTANT", "Hello world."); Echo CONSTANT; Outputs "Hello world." Echo Constant; Output "Constant" and issue a hint level error message?>
Example #2 Use the keyword const to define constants <?php//The following code will work correctly after PHP 5.3.0 const CONSTANT = ' Hello world '; Echo CONSTANT; >

Magic Constants

The 8 "Magic constants" of PHP change as they change position in the code.

    the current line number in the __line__ file.    the full path and file name of the __file__ file. If used in the included file, returns the file name that is included. From PHP 4.0.2, __file__ always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path. The directory where the __dir__ file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to DirName (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (added in PHP 5.3.0)   The name of the __class__ 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, Foo\bar). 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.  the method name of the __method__ class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive). __namespace__   The name of the current namespace (case sensitive). This constant is defined at compile time (PHP 5.3.0 is new).
/*__function__ and __method__ as in PHP 5.0.4 are that__function__ returns only the N Ame of the Functionwhile as __method__ returns the name of the class alongwith the name ofthe function*/<?phpclass tric      k{function doit () {echo __function__;      } function Doitagain () {echo __method__; }} $obj =new trick (), $obj->doit ();//output would be----doit$obj->doitagain ();//output would be-----trick::d Oitaga In 
Related Article

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.