Php constants. Naming rules for constants: the same naming rules as variables: define () function format: define (constant name, value); example: define (PI, 3.14 ); constants and variables are the same: 1: naming rules for naming constants
Naming: The same naming rules as variables
Method: define () function
Format: define ('constant name', 'Specific Value ');
Example: define ('pi ', 3.14 );
Comparison between constants and variables
Same:
1: The naming rules are the same (but generally in uppercase)
Different:
1: $ is not used when a constant is referenced, and the name is used directly.
2: constants can only be defined using define, but cannot be defined using a value assignment statement.
3: once defined, constants cannot be redefined or undefined.
4: the constant value can only be a scalar (only integer, floating point, Boolean, numeric, or NULL)
[Php]
// Define a constant
Define ('pi ', 3.14 );
// Area of the computing Park
$ R = 3;
Echo pi * $ r;
// Result: 28.26
// After a constant is defined, it cannot be redefined.
Define ('pi ', 3.23 );
// Result: Notice: Constant pi already defined in C: \ wamp \ www \ 0124 \ 05.php on line 15
// The constant cannot be assigned a value again.
Pi = 100;
Echo pi;
// Result: Parse error: syntax error, unexpected '= 'in C: \ wamp \ www \ 0124 \ 05.php on line 21
// Syntax errors
$ PI = 10;
Function text (){
Echo $ PI; // The function has a scope. This $ PI variable is not the same as $ PI in the function.
Echo pi; // A constant defined once, which can be used anywhere
}
// Result: Notice: Undefined variable: PI in C: \ wamp \ www \ 0124 \ 05.php on line 31
Text ();
?>
How can I determine whether a constant is defined?
Define defines constants
Defined judge constant
[Php]
If (defined ('pi ')){
Echo 'pi constant has been defined.
';
} Else {
Echo 'pi constant is undefined. let me define it.
';
Define ('pi ', 33322 );
}
Echo PI;
?>
[Php]
Result: The PI constant is undefined. I will define it.
33322
Naming rule: the same naming rule method as the variable: define () function format: define (constant name, specific value); example: define (PI, 3.14 ); constants and variables are the same: 1: name...