Define php constants and use php constants
- Define ("PI", 3.14); defines a constant
- $ Area = PI * R; calculate the area of the circle
- Define ("URL", "http://bbs.it-home.org ");
- Echo "my website url is:". URL;
- ?>
2,System constantFILE: php program FILE name LINE: PHP program FILE number PHP_VERSION: current parser version number PHP_ OS: the operating system name for executing the current PHP version can be used directly, for example, to view the name of the operating system running the current PHP version, you can write it as echo PHP_ OS 3,Php constants Constants can be defined in the class. The constant value remains unchanged. You do not need to use the $ symbol when defining and using constants. The constant value must be a fixed value and cannot be a variable, class attribute, or result of other operations (such as function call. Constants can also be defined in interfaces. Please refer to the interface documentation for more examples. After PHP5.3.0, you can use a variable to dynamically call the class. However, the value of this variable cannot be self, parent, or static. Example 1: define and use a class constant
Class MyClass
- {
- Const constant = 'constant value ';
- Function showConstant (){
- Echo self: constant. "\ n ";
- }
- }
Echo MyClass: constant. "\ n ";
$ Classname = "MyClass ";
- Echo $ classname: constant. "\ n"; // after PHP 5.3.0
$ Class = new MyClass ();
- $ Class-> showConstant ();
Echo $ class: constant. "\ n"; // after PHP 5.3.0
- ?>
Example 2: static data
- Class foo {
- // After PHP 5.3.0
- Const bar = <'eot'
- Bar
- EOT;
- }
- ?>
|