php constants define and use php constants

WBOY
Release: 2016-07-25 09:04:21
Original
1090 people have browsed it
  1. define("PI",3.14);Define a constant
  2. $area = PI*R*R; Calculate the area of ​​the circle
  3. define("URL","http://bbs. it-home.org");
  4. echo "my website url is:".URL;
  5. ?>
Copy code

2, System constants FILE:php program file name LINE: Number of lines of PHP program file PHP_VERSION: The version number of the current parser PHP_OS: The name of the operating system that executes the current PHP version It can be used directly. For example, if you want to check the name of the operating system running the current PHP version, you can write echo PHP_OS

3, php class constants

Constants can be defined in classes. The value of a constant will always remain the same. There is no 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 attribute or other operation (such as a function call). Constants can also be defined in interfaces. See the interface's documentation for more examples.

After PHP5.3.0, you can use a variable to dynamically call a class. But the value of this variable cannot be the keywords self, parent or static.

Example 1. Define and use a class constant

  1. class MyClass

  2. {
  3. const constant = 'constant value';
  4. function showConstant() {
  5. echo self::constant . "n";
  6. }
  7. }

  8. echo MyClass::constant . “n”;

  9. $classname = “MyClass”;

  10. echo $classname::constant . “n”; / / After PHP 5.3.0

  11. $class = new MyClass();

  12. $class->showConstant();

  13. echo $class::constant ."n"; //After PHP 5.3.0

  14. ?>

Copy code

Example 2, static data

  1. class foo {
  2. //After PHP 5.3.0
  3. const bar = <<<'EOT'
  4. bar
  5. EOT;
  6. }
  7. ?>
Copy Code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!