1. Custom Constants
* Must be defined with function define ()
* The value can not be changed after the definition is finished
* Use the constant name directly, not as a variable in front plus $s
For example: Define ("PI", 3.14); Define a constant
$area = Pi*r*r; Calculate the area of a circle
Define ("URL", "http://www.jb51.net");
echo "My URL is:". URL;
2 System constants:
File:P hp program filename
Line:P number of HP program files
Php_version: Version number of the current parser
Php_os: Run the current PHP version of the operating system name
You can use it directly, for example, to see the name of the operating system that executes the current PHP version, you can write the echo php_os
PHP defines and uses a class constant
PHP class Constants
We can define constants in the class. The value of the constant will always remain unchanged. You do not need to use the $ symbol when defining and using constants.
The value of a constant must be a fixed value and cannot be the result of a variable, class property, or other operation, such as a function call.
Its also possible for interfaces to have constants. Look in the interface documentation for examples. You can also define constants in the interface (interface). See the documentation for the interface for more examples.
After PHP5.3.0, we can use a variable to invoke the class dynamically. However, the value of the variable cannot be the keyword self, parent, or static.
Define and use a class constant
Copy Code code as follows:
<?php
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 sample
Copy Code code as follows:
<?php
class Foo {
After PHP 5.3.0
Const BAR = <<< ' EOT '
Bar
EOT;
}
?>