Home>Article>Backend Development> Summary of how to use php constants
This article mainly introduces the relevant information on the knowledge ofconstantsin PHP. Friends in need can refer to the following
Constants in PHP are divided into custom constants and System constants
Characteristics of constants:
After setting, the value of the constant cannot be changed
Constant name The leading dollar sign ($) is not required
Scope does not affect access to constants
Constant values can only becharacters Stringor number
Get the value of a constant:
Use the constant name to get the value directly;
Use ==constant()==Function, it has the same effect as directly using the constant name to output, but the function can dynamically output different constants, so you need to use Flexible and convenient.
defined() function: Determine whether a constant has been defined
define("PI",3.14); $is1 = defined('PI'); $is2 = defined('PI1'); var_dump($is1); // boolean true var_dump($is2); // boolean false
1. Custom constant
In PHP, use the ==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 are:
1.FILE: PHP program file name. It can help us get the physical location of the current file on the server.
2. LINE: The number of lines in the PHP program file. It can tell us which line the current code is on.
3. PHP_VERSION: The version number of the current parser. It can tell us the version number of the current PHP parser, and we can know in advance whether ourPHP codecan be parsed by the PHP parser.
4. PHP_OS: The name of the operating system that executes the current PHP version. It can tell us the name of the operating system used by the server and we can optimize our code according to that operating system.
echo FILE; // E:\Web\Project\_wamp\demo.php echo LINE; // 3 echo PHP_VERSION; // 5.6.25 echo PHP_OS; // WINNT
The above is the detailed content of Summary of how to use php constants. For more information, please follow other related articles on the PHP Chinese website!