In php, three staticconstdefine variables can all be called static expressions, meaning that the defined variables cannot be changed, but the three variables are for different domain names, let me introduce you to the three differences. const is used to determine... in php, the three static const define variables can be called static expressions, meaning that the defined variables cannot be changed, but the three of them are for different domain names, let me introduce you to the three differences.
Const
It is used to define the constant values that cannot be changed at runtime. once defined, it cannot be changed. if the value is changed in the program, an error occurs. the code is as follows:
Without the dollar sign ($), it can be used without class initialization. a constant value must be a constant expression, a variable defined by the const, and can be overwritten in the subclass.
The internal definition of the class can also be defined outside the class after php5.3. const cannot use expressions.
Define ()
You 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.
Define defines global constants and can be accessed anywhere.
Define cannot be defined in the class but const can.
Define can use any expression, such as define ('bit _ 5', 1 <5 );
The code is as follows:
The difference between define () and const:
In memory space allocation, when define is macro-defined, it will not allocate memory space. during compilation, it will be replaced in the main function. it is just a replacement and will not be checked, such as the type, statement structure, that is, macro-defined constants are purely placement relationships, such as # define null 0; when the compiler encounters null, it always uses 0 instead of null. it has no data type. (For more information, see C language books for preprocessing or MSDN. the constants defined by const have data types. constants defining data types are convenient for the compiler to perform data checks, so that programs may encounter errors for troubleshooting, therefore, the difference between const and define is that const defines constants to eliminate the security of programs.
Define defines global constants and can be accessed anywhere.
Const is used for class member variable definition. it can only be accessed by class name and cannot be changed. if you are a beginner, you can simply understand it.
Static
Another important feature of the range of variables in php is static variables ).
Static variables exist only in the local function domain and are initialized only once. when the execution of the program leaves this scope, the value of the static variables will not disappear and the result of the last execution will be used.
Static keywords in a class describe a member to be static. static can restrict external access, because the static members belong to the class and do not belong to any object instance.
Static members must be accessed using self. the code is as follows:
Class Counter {public static $ count = 0; // defines a static property // defines a static method static function getCount () {return self ::$ count ;}} echo Counter: getCount (); Counter: count;The following is an example:
Name; $ this-> set_name () ;}$ const1 = new test (); // 1 $ const1-> say_hello (); // 2 echo"
"; Echo test: CHARSET; // 3 echo"
"; Echo $ const1-> name; // 4 echo"
"; Echo $ const1-> set_name (); // 5 echo"
"; Echo $ const1-> set_name2 (); // 6 echo"
"; Echo test: CHARSET; // 7 echo"
"; Echo $ const1-> CHARSET;
Permanent link:
Reprint at will! Include the article address.