What is the usage of const in php

青灯夜游
Release: 2023-03-13 19:34:01
Original
7099 people have browsed it

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 常量名 = 常量值;
Copy after login

Example:

<?php
    const url = &#39;https://www.php.cn/&#39;;
    echo url;
?>
Copy after login

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!

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