A detailed explanation of the differences between define and const in PHP

Source: Internet
Author: User
Tags php define scalar

Everyone should know that define and const are constants in PHP, so how much do you know about them? This article mainly and everyone to share PHP define and the difference between the const detailed, hope to help everyone.

The understanding of constants:

  1. The constant is preceded by a dollar sign ($);

  2. Constants can only be defined with the Define () function, not through assignment statements;

  3. Constants can be defined and accessed anywhere, regardless of the scope of the variable;

  4. Once a constant is defined, it cannot be redefined or undefined;

  5. The value of a constant can only be scalar.

The difference between constant definitions:

Use of const use of define
Const for the definition of a class constant Define cannot be used for definitions of class constants and can be used for global constants.
Constants cannot be defined within a function, within loops, and within an if statement with Const. Define can
Const uses a common constant name Define can use an expression as a name
Const can only accept static scalar Define can take any expression
Const-defined constants are case-sensitive Define can specify case sensitivity by using the third argument, true for case insensitivity.
Related Article

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.