Home >Backend Development >PHP Problem >What is the usage of const in php

What is the usage of const in php

青灯夜游
青灯夜游Original
2021-11-10 18:31:047130browse

const is a keyword in PHP, which is used to define constants. The syntax is "const constant name = constant value;"; when the const keyword defines a constant, it can only contain scalar data (bool, int, float, string). Once a constant is defined, it cannot be changed or undefined.

What is the usage of const in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

const is a keyword in php, is Used to define constants.

In PHP, 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. 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.

Compared with variables, constants have the following characteristics:

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

  • Constants can only be defined using define() and const;

  • The scope of constants is global;

  • Once defined, constants cannot be redefined or undefined.

The syntax of const keyword to define constants:

const 常量名 = 常量值;

Example:

<?php
    const url = &#39;//m.sbmmt.com/&#39;;
    echo url;
?>

What is the usage of const in php

Recommended study:《PHP video tutorial

The above is the detailed content of What is the usage of 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