Define php constants and use php constants

Source: Internet
Author: User
Define php constants and use php constants

  1. Define ("PI", 3.14); defines a constant
  2. $ Area = PI * R; calculate the area of the circle
  3. Define ("URL", "http://bbs.it-home.org ");
  4. Echo "my website url is:". URL;
  5. ?>

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

  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. ?>

Example 2: static data

  1. Class foo {
  2. // After PHP 5.3.0
  3. Const bar = <'eot'
  4. Bar
  5. EOT;
  6. }
  7. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.