Constants are a very new data type in php. I will introduce some usage of PHP constants in detail for beginners. For more information, see. PHP constant define () function is used to define constants. once a constant is defined, it cannot be changed... constants are a very new data type in php. I will introduce some usage of PHP constants in detail for beginners. For more information, see.
PHP constants
The define () function is used to define constants. once a constant is defined, it cannot be changed or canceled.
The instance code for defining constants is as follows:
Constant names and any other PHP labels follow the same naming rules. valid constant names start with letters or underscores followed by any letters, numbers or underscores.
Constants are case-sensitive by default, and constant identifiers are always capitalized. The value cannot be changed during script execution. differences between defining constants and defining variables:
1. there is no dollar sign before the constant ($)
2. constants can only be defined using the define () function, but not through the value assignment statement.
3. constants can be defined and accessed anywhere regardless of the variable range rules.
4. once defined, constants cannot be redefined or undefined.
5. the constant value can only be a scalar.
PHP has a large number of predefined constants built in. you can search for the PHP Manual on the Internet for specific content.
Determines whether a constant has been defined.
How to determine whether a php constant has been defined, suddenly confused, dizzy, specially checked the manual, the record filing summary results are as follows:
(1) determine whether a constant exists
The instance code is as follows:
if(defined('MYCONSTANT')){ echo MYCONSTANT; }
(2) determine whether a variable is defined
The instance code is as follows:
If (isset ($ myvar) {echo "variable $ myvar .";}
(3) determine whether a function exists
The instance code is as follows:
Differences between constants and variables:
1: constants are globally valid, so on the page, within the function, the class or even the array can be directly referenced.
The instance code is as follows:
$ A = 66; function t () {echo $ a;} t (); // at this time, 99 cannot be printed because of the impact of function scope. to print 99, can be changed to: define ("A", 66); function t () {echo A;} t ();
2: once a constant is defined, it cannot be redefined. it cannot be cleared or modified. constants can also be dynamic.
The instance code is as follows:
Define ("A", "constant introduction"); define ("B", "constant Dynamic Call"); $ c =$ _ get ['c']; // the value of B is directly used here, and the value of B is no longer considered as a constant name. echo constant ($ c) is parsed again; // constant (constant name) ---> returns the value of a constant.
Const, a constant modifier commonly used in object-oriented const constant modifiers. we know that defining constants in PHP is done through the define () function, but defining constants in the class cannot use define (), but the const modifier must be used. after a constant in a class is defined by const, its access method is similar to that of a static member. it is accessed by the class name or by using self in the Member method, however, PHP 5.3.0 and later versions can also be accessed using objects. A constant defined by const cannot be assigned a value again. if you try to change its value in a program, an error will occur.
The instance code is as follows:
"; // Use self to access constants. do not add" $ "} echo MyClass: CONSTANT ."
"; // Use the class name to access the constant outside the class, and do not add" $ "$ class = new MyClass (); $ class-> showConstant (); echo $ class :: CONSTANT; // after PHP 5.3.0?>
Follow-up details: You do not need to use the "$" symbol before using the constant name defined by const, and the constant name is usually in uppercase.
An error occurs when you try to assign a value to a constant defined by const.
The instance code is as follows:
CONSTANTS and PHP Class Definitions
Using "define ('My _ var', 'default value')" INSIDE a class definition does not work. you have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, or string (no array or other object types) -- right away.
You cannot use "define ('My _ var', 'default value')" in the class to define constants. you must use the PHP keyword 'const' to initialize a scalar-boolean, int, float, or string (except for arrays and other object types ),
The instance code is as follows:
#Example 1: You can access these constants DIRECTLY like so: * type the class name exactly. * type two (2) colons. * type the const name exactly. #Example 2: Because our class definition provides two (2) static functions, you can also access them like so: * type the class name exactly. * type two (2) colons. * type the function name exactly (with the parentheses).
The instance code is as follows:
Once class constants are declared AND initialized, they cannot be set to different values -- that is why there are no setMinValue() and setMaxValue() functions in the class definition -- which means they are READ-ONLY and STATIC (shared by all instances of the class).
When class constants are declared and initialized, they cannot be set to other values -- that is why they do not have setMinValue () and setMaxValue () when defining classes () these two methods -- both of them are read-only and static (shared by all objects of this class ).
Address:
Reprinted at will, but please attach the article address :-)