{PHP constant}

Source: Internet
Author: User
Tags scalar variable scope
Constant

A constant is an identifier (first name) of a simple value. As the name implies, the value cannot be changed during script execution (except for the so-called magic constants, they are not constants). Changshime is considered to be case sensitive. Usually, constant identifiers are always uppercase.

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: [a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*

Example #5 Legal and illegal constant names

                               


Note: Here, the letters are a-z,a-z, as well as ASCII characters from 127 to 255 (0X7F-0XFF).

As with Superglobals, the range of constants is global. You can access constants anywhere in the script without the scope of the control. For more information on the role, please read the variable range in the manual.

Define defines global constants that can be accessed everywhere.
A const is a global variable that defines a class and cannot change its value.

                  

Say_hello (); ? >


As a further explanation, see the following extended reading:

Extended reading: There is no dollar sign ($) in front of constants, constants can only be defined with the Define () function, not through assignment statements; constants are defined and accessed anywhere without regard to the rules of variable scope; constants cannot be redefined or undefined once defined; constants can only be scalar; Constants can only contain scalar data (boolean,integer,float and string), and do not define resource constants. You can use the function constant () to read the value of a constant. Get_defined_constants () can get a list of all defined constants. If you use an undefined constant, PHP assumes that you want the name of the constant itself, as if it were called with a string (CONSTANT corresponds to "CONSTANT"), and a e_notice-level error is emitted. PHP's "Magic Constants".

--------------------------------------------------------------------------------------------------------------- -------------------------------------------

Magic Constants

PHP provides a large number of predefined constants to any script it runs. 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.

There are seven magic constants whose values change as they change position in the code. For example, the value of __line__ depends on the row it is in the script to determine. These special constants are case-insensitive, as follows:

Several "Magic constants" for PHP Name Description
__line__ The current line number in the file.
__file__ The full path and file name of the 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.
__dir__ 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 DirName (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0) =
__function__ 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.
__class__ 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.
__method__ The method name of the 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 new)

Echo __file__;

Echo __class__;

Echo __file__;
?>

See Get_class (), Get_object_vars (), file_exists () and function_exists ().

  • 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.