Home  >  Article  >  Backend Development  >  What is the difference between constants and variables in php?

What is the difference between constants and variables in php?

青灯夜游
青灯夜游Original
2019-11-21 16:18:292635browse

What is the difference between constants and variables in php?

What are php constants?

The so-called constant is a quantity that cannot be changed. Once a PHP constant is defined, it cannot be changed or undefined. This is the official explanation and the most authoritative explanation. PHP constants are usually used to store data that does not change and is not expected to change. The data can only be data of four scalar data types: integer (integer), floating point (decimal), string, Boolean (true) /false).

The difference between constants and variables in php

Different definition methods:

php variables have no defined keywords or functions and are assigned when they are assigned definition. PHP constants must be defined using the define() function.

Different names:

php variable names need to be modified with a dollar sign ($), but php constant names do not. It is recommended that PHP variable names be lowercase. When they consist of multiple words, the first letter of the first word should be lowercase, and the first letter of the remaining words should be uppercase. It is recommended that PHP constant names be in all uppercase letters. The meanings of

values ​​are different:

The value stored by a php variable can be changed, while the value stored by a php constant cannot be changed.

Scope is not used:

php variables are defined outside all functions and have global scope; when defined within functions, they have local scope. PHP constants are generally defined outside all functions and have super-global scope.

The data type range is different:

php variables can be of any data type, while php constants can only be scalar types (integer, float/double, string, boolean).

Recommended learning: PHP tutorial

The above is the detailed content of What is the difference between constants and variables 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