Home >Backend Development >PHP Problem >How to define constants in php?

How to define constants in php?

autoload
autoloadOriginal
2021-03-11 11:51:353519browse

As we all know, constants are the same as <span style="font-family:monospace">variables</span>, both are used to save data.

1. The basic concept of constants

const/constant: It is an unchangeable quantity (data) during the running of the program;

Once a constant is defined, it usually cannot be modified;

After a variable is defined, it can be modified under normal circumstances unless it is

final modifier. (About the final modifier "Final's Journey in this Life")

2. The form of constant definition

a. Use the function that defines the constant :

define ('constant name', constant value)

<?php
   define(&#39;A&#39;,10);
   echo A;
?>

b.

constKeyword

<?php
  const A=10;
   echo A;
?>

3. Naming of constants Rules

  • # Constants do not need to use the "$" symbol, once used they are considered variables.

  • Variable names can be named by numbers, letters, and underscores, but they must start with letters and underscores.

  • The names of constants are usually in uppercase letters (to distinguish them from variables).

  • The rules for variable naming are looser than variables, and some special characters (define function) can be used.

  • Variables are usually not case-sensitive, but they can be (the third parameter of the define function).

Recommended:

php tutorial,php video tutorial

The above is the detailed content of How to define constants 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