The difference between static const define variables in PHP

Source: Internet
Author: User
Tags constant define null

Const

Used to define constant values that cannot be changed at run time. Once defined and cannot change its value, an error occurs if the value is changed in the program.

The code is as follows Copy Code

<?php
CALSS math{
Const pi=3.14159;
}
echo Math::p i;
?>

There is no dollar sign ($) before and can be useful without initializing the class. The constant value must be a constant expression. const-defined variables that can be overridden in subclasses.
Defined inside a class, php5.3 can also be defined outside the class.
Const can not use an expression.

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 the constant by specifying its name, and do not precede the constant with the $ symbol.
Define defines global constants that can be accessed anywhere.
Define cannot be defined in a class and const can.
Define can take any expression. such as define (' Bit_5 ', 1 << 5);

The code is as follows Copy Code

Define ("CONSTANT", "Hello world.");
Echo CONSTANT; Outputs "Hello world."
Echo Constant; Outputs "Constant" and issues a notice.
? >


the difference between define () and const ():

Allocation of memory space. Define for macro definition, will not allocate memory space, the compilation will be in the main function in the replacement, but simply replace, will not do any checks, such as type, sentence structure, that is, macro-defined constants is simply a placement relationship, such as #define NULL 0; The compiler always uses 0 instead of NULL when it encounters null it has no data type (there are questions please find the C language book to see the preprocessing section or see MSDN.) const-defined constants have data types, and constants that define data types make it easier for the compiler to check for data, making it possible for the program to troubleshoot. So the difference between a const and a define is that the const definition constant excludes the security between programs.
Define defines global constants that can be accessed anywhere
Const is used for class member variable definition, can only be accessed with class name cannot be changed if beginners are so understanding, just don't be too obsessed.

Static

Another important feature of the scope of variables in PHP is static variables.
A static variable exists only in the local function field and is initialized once, and the value does not disappear when the program executes away from the scope, using the result of the last execution.
The Static keyword in a class describes a member as static, and static can restrict access to the outside because the static member belongs to the class and is not part of any object instance.
Static members that must be accessed using self.

The code is as follows Copy Code

Class Counter
{
public static $count = 0;//defines a static property
To define a static method
static function GetCount ()
{
Return self:: $count;
}

Echo Counter::getcount ();
Counter::count;

Finally, add an example:

The code is as follows Copy Code

<?php


Echo HAHA;

Class Test {
Public $name 2 = ' name2 ';
static $name = ' name ';
Const CHARSET = "China";
Public Function Say_hello () {
echo self:: CHARSET;
}

public static function Set_name () {
echo self:: $name;
}

public static function Set_name2 () {
Self::set_name ();
}

public static function Set_name3 () {
Echo $this->name;
$this->set_name ();
}

}
$const 1 = new test ();

1
$const 1->say_hello ();

2
echo "<br/>";
echo Test:: CHARSET;

3
echo "<br/>";
Echo $const 1->name;

4
echo "<br/>";
echo $const 1->set_name ();

5
echo "<br/>";
echo $const 1->set_name2 ();

6
echo "<br/>";
Echo Test::charset;

7
echo "<br/>";
Echo $const 1->charset;
?>

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.