Constants must be defined before use, otherwise an error will occur during program execution. Use define() function in php tutorial to define constants.
1. Syntax format: define("constant name", "constant value");
For example: define("php360","Perfect php");
Here’s an example:
define() function defines a constant.
Constants are similar to variables, except that:
After setting, the value of the constant cannot be changed
Constant names do not require a leading dollar sign ($)
Scope does not affect access to constants
Constant values can only be strings or numbers
Grammar
define(name,value,case_insensitive) parameter description
name required. Specifies the name of the constant.
value required. Specifies the value of the constant.
case_insensitive required. Specifies whether constant names are case-sensitive.
If set to true, it will not be case sensitive. Default is false (case sensitive).
*/
define("greeting","hello world!");
echo constant("greeting");
/*
Running this code will output the result hello world! to the browser.
2. Rules for constant naming: start with a letter or underscore, and can be followed by any letter, number, or underscore.
3. The difference between constants and variables:
(1) There is no $ sign in front of the constant, but the variable must start with the $ sign.
(2) Constants can only be defined using the define() function and cannot be defined through assignment statements.
(3) Constants can be defined and accessed anywhere regardless of the rules of variable scope.
(4) Once a constant is defined, it cannot be redefined or undefined, and its value cannot be changed, while the value of a variable can change at any time.
(5) The value of a constant can only be a scalar, that is, three types: integer, floating point, and string*/