Home > Article > Backend Development > The difference between php variables and constants
Constant:
Identifier used to store a data that will not change and is not expected to change (the naming rules are the same as variables)
Definition form: (Recommended learning: PHP video tutorial)
Use define() function definition
Use form: define ("Constant name", constant value)
Use counst syntax definition
Usage form: counst constant name = constant value
Use constants: There are two forms 1. Use the name directly 2. Use the constant() function: constant ("constant name")
Use constants: There are two forms 1. Use the name directly 2. Use the constant () function: constant ("constant name")
The difference between constants and variables:
Definition form Different (php variables have no defined keywords or functions and are defined when assigning values. PHP constants must be defined using the define() function);
The usage form is different (constants do not require the $ symbol);
Different degrees of variability (the value of a constant cannot be changed or destroyed);
Different scopes (constants have super-global scope (can be used directly inside and outside the function));
The available types are different (constants can only store scalar types: int, float, string, bool);
The above is the detailed content of The difference between php variables and constants. For more information, please follow other related articles on the PHP Chinese website!