Home > Article > Backend Development > Summary of knowledge about constants in PHP
This article mainly introduces relevant information about constant knowledge in PHP. Friends who need it can refer to it
Constants in PHP are divided into custom constants and system constants
Characteristics of constants:
After setting, the value of the constant cannot be changed
The constant name does not require a dollar sign at the beginning ($)
Scope does not affect access to constants
Constant values can only be strings or numbers
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 it must be flexible and convenient to use.
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 constantdefine("PI",3.14); echo constant('PI'); define("GREETING","Hello world!",TRUE); echo constant('greeting');2. System constantsCommon 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 our PHP code can 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; // WINNTThe above is the entire content of this article, I hope it will be helpful to everyone's study.
PHPHow to use curl to simulate post uploading and receiving files_php tips
php mailer class calls the remote SMTP server to send mail implementation method_php skills
The above is the detailed content of Summary of knowledge about constants in PHP. For more information, please follow other related articles on the PHP Chinese website!