PHP constant knowledge sorting, php constant knowledge sorting

Source: Internet
Author: User

PHP constant knowledge sorting, php constant knowledge sorting

Constants in PHP are divided into custom constants and system constants.

Constant features:

  1. After setting, the constant value cannot be changed.
  2. The constant name does not need to start with the dollar sign ($)
  3. The scope does not affect constant access.
  4. A constant value can only be a string or number.

Get the constant value:

  • Use the constant name to directly obtain the value;
  • Use = constant () = function. It outputs the same effect as directly using a constant name, but the function can dynamically output different constants, it is flexible and convenient to use.

Defined () function: determines whether a constant has been defined.

define("PI",3.14);$is1 = defined('PI');$is2 = defined('PI1');var_dump($is1); // boolean truevar_dump($is2); // boolean false

1. Custom Constants

In PHP, use = define () = function to define a constant.

define("PI",3.14);echo constant('PI');define("GREETING","Hello world!",TRUE);echo constant('greeting');

2. System Constants

Common system constants include:

1. _ FILE _: php program FILE name. It helps us to obtain the physical location of the current file on the server.

2. _ LINE _: Number of PHP program files. It tells us the number of lines in the current Code.

3. PHP_VERSION: version number of the current parser. It tells us the version number of the current PHP parser. We can know in advance whether our PHP code can be parsed by the PHP parser.

4. PHP_ OS: name of the operating system for executing the current PHP version. It tells us the name of the operating system used by the server, and we can optimize our code based on the operating system.

echo __FILE__; // E:\Web\Project\_wamp\demo.phpecho __LINE__; // 3echo PHP_VERSION; // 5.6.25echo PHP_OS; // WINNT

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.