Syntax analysis of PHP constants and constants

Source: Internet
Author: User
Tags constant definition php define scalar variable scope
This article mainly introduces the PHP constant, the constant syntax analysis, the interest friend's reference, hoped to be helpful to everybody.

One, 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. By convention constant identifiers are always capitalized.
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]* (Note: Here, the letters are a-z,a-z, and ASCII characters from 127 to 255 (0X7F-0XFF))
As with super Globals, 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.

Second, the grammar
You can use the Define () function to define constants. Once a constant is defined, it cannot be changed or undefined during the execution of the program.
Constants can only contain scalar data (boolean,integer,float and string).
You can simply get the value of the constant by specifying its name, and do not precede the constant with the $ symbol. If the constant name is dynamic, you can also use the function constant () to read the value of the constant. A list of all defined constants can be obtained with get_defined_constants ().
Note: Constants and (global) variables are in different namespaces. This means, for example, that TRUE and $TRUE are different.
If you use an undefined constant, PHP assumes that you want the name of the constant itself, as if you were calling it with a string (CONSTANT corresponds to "CONSTANT"). An error of E_notice level is issued at this time. See the manual why $foo [bar] is wrong (unless you define bar as a constant in advance with define ()). If you only want to check if a constant is defined, use the defined () function.

Iii. differences in constants and variables
The constant is preceded by a dollar sign ($);
Constants can only be defined with the Define () function, not through assignment statements;
Constants can be defined and accessed anywhere without regard to the rules of variable scope;
Once a constant is defined, it cannot be redefined or undefined;
The value of a constant can only be scalar.
Example of a constant definition:

<?php define ("CONSTANT", "Hello World"); Echo CONSTANT; Outputs "Hello world" echo Constant; Outputs "Constant" and issues a notice. ?>

Iv. pre-defined 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 four magic constants that change depending on where they are used. 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:
The current line number in a few PHP "Magic constants"__line__ files.
The full path and file name of the __file__ file.
__function__ function name (this is the new addition of PHP 4.3.0).
The name of the __class__ class (this is a new addition to PHP 4.3.0).
The method name of the __method__ class (this is a new addition to PHP 5.0.0).

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.