Constantcan be understood as: a permanent value
After a constant value is defined, it cannot be changed anywhere else in the script
#PHP Constants
Note: Unlike variables, constants are automatically global throughout the entire script.
#Set PHP constants
To set a constant, use the define() function - it uses three parameters:
1. The first parameter defines the name of the constant
2. The second parameter defines The value of the constant
3. The optional third parameter specifies whether the constant name is case-sensitive. The default is false.
Example
The following example creates a case-sensitive constant whose value For "Welcome to PHP.cn!":
<?php define("GREETING", "Welcome to PHP.cn!"); echo GREETING; ?>
You can change the constant name of echo above to lowercase and try to see what will be output
##ExampleThe following example creates a size-insensitive constant with the value "Welcome to PHP.cn!":
<?php define("GREETING", "Welcome to PHP.cn!",true); echo greeting; ?>
After a constant is defined, it defaults to a global variable and can be used in the entire running script. Use anywhere.
The following example demonstrates the use of constants within a function, even if the constant is defined Constants can also be used normally outside functions.
<?php header("Content-type:text/html;charset=utf-8"); define("GREETING", "欢迎访问 php.cn"); function myTest() { echo GREETING; } myTest(); // 输出 "欢迎访问 php.cn" ?>
In addition, the system also prepares some built-in constants for us. These constants are specified. Let’s get familiar with a few first, and there are more system constants that can be slowly added and learned after getting started.
Description | LINE | ||||||||||||
FILE | |||||||||||||
FUNCTIOIN | |||||||||||||
CLASS | |||||||||||||
Current member method name | |
The operating system that PHP runs on | |
Current PHP version | |
Trait Name, added in php5.4 | |
The directory where the file is located |
The name of the current namespace (case sensitive) |