Differences between staticconstdefine variables in php

Source: Internet
Author: User
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.

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.