Difference explanation
1. Const is a language structure, define() is a function, and const compilation is faster than define().
2. define() declares a constant. The constant name is of string type, can be dynamically spliced, and can be named with an expression. Const can only use ordinary constant names.
Constants defined by the const keyword are case-sensitive, and the define() function can determine whether it is case-sensitive through the third parameter.
Example
<?php // Constants // Definition and use of constants // Definition method 1: define() function define("CON_INT", 100); echo CON_INT; // Define the use of this constant to be case-insensitive define("GREETING", "hello world", true); // For this kind of usage, the system will give a reminder that it is not recommended echo GREETING; echo Greeting; echo "<br/>"; //Definition method 2: const keyword definition const FOO = 'BAR'; for($i = 0; $i <32; ++$i){ define('YDMA_'.$i, 1 + $i); } echo YDMA_16; // const cannot define constants in conditional statements/* if(true){ const FOO0 = 'BAR'; // invalid} if(true){ define('FOO0', 'BAR'); // valid} */ // Get the value of the constant: constant() function echo "<br/>"; define("VAR0", "888"); echo constant("VAR0"); echo "<br/>"; const CONSTANT0 = 'test contant'; echo constant("CONSTANT0"); // Get a list of all defined constants echo "<pre>"; print_r(get_defined_constants()); // Get all the constants that can be accessed in this script and output an extra long array
The above is the difference between the methods of defining constants in PHP. I hope it will be helpful to everyone.