What does const mean in php?

藏色散人
Release: 2023-03-14 06:34:01
Original
2658 people have browsed it

const in php is a keyword used to define a constant; when using the const keyword to define a constant, it can only contain scalar data; the constant can be defined as an expression or as an array.

What does const mean in php?

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3.

What does const mean in php?

const in php is a keyword used to define a constant.

You can use the const keyword or the define() function to define a constant. The function define() allows a constant to be defined as an expression, while the const keyword has some restrictions, see the following chapters for details. Once a constant is defined, it cannot be changed or undefined.

When using the const keyword to define a constant, it can only contain scalar data (bool, int, float, string). A constant can be defined as an expression or as an array. You can also define resource as a constant, but this should be avoided as it may cause unpredictable results.

You can get the value of a constant simply by specifying its name. Unlike variables, you should not add the $ sign in front of a constant. If the constant name is dynamic, you can also use the function constant() to get the value of the constant. Use get_defined_constants() to get a list of all defined constants.

Note: Constants and (global) variables are in different namespaces. This means that for example true and $TRUE are different.

If an undefined constant is used, an Error will be thrown. Before PHP 8.0.0, calling an undefined constant would be interpreted as a string of that constant, that is (CONSTANT corresponding to "CONSTANT" ). This method is deprecated in PHP 7.2.0 and will throw an E_WARNING level error. (PHP prior to 7.2.0 would emit an E_NOTICE level error.) See the manual for why $foo[bar] is an error (unless bar is a constant). This does not apply to (fully) qualified constants, which will always raise Error if undefined.

Note: If you want to check whether a constant is defined, please use the defined() function.

The differences between constants and variables are as follows:

  • There is no dollar sign ($) in front of the constant;

  • You can ignore the constant The scope of the variable can be defined and accessed anywhere;

  • Once defined, a constant cannot be redefined or undefined;

  • Constant only Can compute scalar values ​​or arrays.

Note:

Contrary to using define() to define constants, using the const keyword to define constants must be in the topmost scope, because using this method It is defined at compile time. This means that you cannot use const to define constants inside functions, loops, and if or try/catch statements.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does const mean in php?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template