1 Simple Assignment
1 <?php // Preliminary simple to assign a constant value of 2 define('PI ', 3.14); 3 Echo PI
2 Parametric analysis
Define (name, Value,bool)
First parameter
Name constant names are generally named in uppercase.
A second parameter
value To assign a value to a parameter, it must be a scalar (integer, float, Boolean, String type).
First parameter
If the bool constant name is case-insensitive , the default is False when the value is not assigned.
<? define(' NUM ', 3.14,trueecho NUM; // The result of the output is 3.14 Define (' num ', 3.14,falseecho NUM; // The result of the output is num Define (' NUM ', 3.14echo NUM; // The result of the output is num//where the second third is the same situation.
3 Some characteristics of constants
- A constant can only be assigned a value once.
- by definded (), you can detect whether a variable has been assigned a value.
<? PHP Define (' NUM ', 3.14); Var_dump (defined// output is bool (true)
4 The difference between a variable and a constant
- Variables can be assigned directly and constants can only be define () to assign values
- The name of a constant cannot be used in $, while a variable can.
- Constants are not scoped anywhere defined as global constants, and variables have scopes.
- Once a constant is defined, the variable can be modified.
- The value of a constant can only be scalar.
5 new features for PHP 5.3
<? PHP Define (' NUM ', 3.14); Echo NUM; Define ('-_-# ', 3.14); // echo -_-#; Obviously cannot output echoconstant//php5.3 constant can handle this special case
Purely their own learning experience, but also hope that we have a lot of support and suggestions.
PHP constant crash