The difference between const and define in PHP

Source: Internet
Author: User

1 Version differences:

Const requires PHP version "5.3.0

Define can be compatible with PHP4,PHP5 and other versions

2 Definition of location differences:

Constants defined by the Const keyword are defined at compile time, so the const keyword definition constants must be at the top of the range

(meaning that constants cannot be defined within a function, within a loop, and within an if statement with a const)

The constants defined by the function define () are defined when the Define () function is executed, so you can use the Define () function to define constants anywhere within a function, within a loop, within an if statement, or wherever functions can be called.

3 differential support for value expressions:

Const-defined constants are defined at compile time, so many operators, such as arithmetic operators, bitwise operators, comparison operators, and so on, are not supported in expressions for constant values defined by the Const keyword

These operators define() can be used directly when defining constants in a function.

Define (' Define_var1 ', 1 << 1); // Const CONST_VAR1 = (1 << 1);//const does not support bitwise operators, PHP will report syntax errors

4 support differences in case sensitivity to characters:

Const keyword defines constant-size-sensitive

Define () can receive the 3rd parameter, which indicates that the case of the constant name is not sensitive if the argument is True

Define true ); Echo // Output: Codeplayer Echo // Output: Codeplayer Echo // Output: Codeplayer

Note: Using const makes the code easy to read, and const itself is a language structure, and define is a function. In addition, the const is much faster to compile than define.

The difference between const and define in PHP

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.