PHP constant knowledge sorting, php constant knowledge sorting
Constants in PHP are divided into custom constants and system constants.
Constant features:
- After setting, the constant value cannot be changed.
- The constant name does not need to start with the dollar sign ($)
- The scope does not affect constant access.
- 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!