Constant declaration method in PHP

Source: Internet
Author: User
A constant is the identifier (name) of a simple value ). As its name implies, this value cannot be changed during script execution (except for so-called magic constants, they are actually not constants ). Constants are case sensitive by default. By convention, constant identifiers are always capitalized. A constant is the identifier (name) of a simple value ). As its name implies, this value cannot be changed during script execution (except for so-called magic constants, they are actually not constants ). Constants are case sensitive by default. By convention, constant identifiers are always capitalized.
Constant names and any other PHP labels follow the same naming rules. A valid constant name starts with a letter or underline followed by any letter, number, or underline. The regular expression is like this: [a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *
Note: Here the letter is a-z, A-Z, and ASCII characters from 127 to 255 (0x7f-0xff.
Like superglobals, the constant range is global. You can access constants anywhere in the script without specifying the scope. For more information, see the variable range in the manual.
SyntaxYou can use the define () function to define constants. Once a constant is defined, it cannot be changed or undefined. Constants can only contain scalar data (boolean, integer, float, and string ).
You can simply get the value of a constant by specifying its name. do not add the $ symbol before the constant.. If the constant name is dynamic, you can use the constant () function to read the constant value. Use get_defined_constants () to obtain a list of all defined constants.
Note: constants and (global) variables are in different namespaces. This means that for example, TRUE and $ TRUE are different.
If an undefined CONSTANT is used, PHP assumes that you want the name of the CONSTANT, just as you call it with a string (CONSTANT corresponds to "CONSTANT "). At this time, an E_NOTICE-level error will be issued. See why $ foo [bar] is incorrect in the manual (unless you define bar as a constant using define () in advance ). If you only want to check whether a constant is defined, use the defined () function.
Constants and variables are different: 
There is no dollar sign before the constant ($ );
Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;
Constants can be defined and accessed anywhere, regardless of the variable range rules;
Once defined, a constant cannot be redefined or canceled;
The constant value can only be a scalar.

Example 8-1. define a constant

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.