How to define constants in php?

autoload
Release: 2023-03-08 16:32:02
Original
3464 people have browsed it

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;
?>
Copy after login

b.

constKeyword

<?php
  const A=10;
   echo A;
?>
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!