Home  >  Article  >  Backend Development  >  Detailed explanation of the difference between define and const in php

Detailed explanation of the difference between define and const in php

小云云
小云云Original
2018-03-14 09:57:321077browse

Everyone should know that define and const are constants in PHP, but how much do you know about them? This article mainly shares with you a detailed explanation of the difference between define and const in PHP, hoping to help everyone.

Understanding of constants:

  1. There is no dollar sign ($) in front of the constants;

  2. Constants can only be used define() function definition, not through assignment statements;

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

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

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

The difference between constant definitions:

Use of const Use of define
const is used for the definition of class constants Define cannot be used for the definition of class constants, but can be used for global constants.
You cannot use const to define constants within functions, loops, and if statements. define can use an ordinary constant name
const define can use an expression as the name
const can only accept static scalars define can use any expression
const-defined constants are case-sensitive define can use the first Three parameters (true indicates case insensitivity) to specify whether case sensitivity is present.

Related recommendations:

Detailed examples of the difference between the usage of php constants define and const

The above is the detailed content of Detailed explanation of the difference between define and const in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn