PHP defines the array constant (array constant) method, array constant. PHP defines the array constant (array constant) method. in PHP, we cannot use const to directly define the array constant, but const can define the string constant, combined with eval () function PHP defines the array constant (array constant) method, array constant
In PHP, we cannot use const to directly define array constants, but const can define string constants and use the eval () function to execute string constants. Therefore, we can return an array constant by defining a string constant. Here is the time to witness the miracle!
The code is as follows:
<? Php
Class Test
{
Const MY_ARR = "return array (\" a \ ", \" B \ ", \" c \ ", \" d \");";
Public function getConstArray ()
{
Return eval (Test: MY_ARR); // The eval () function executes the string as the PHP code.
}
}
$ T = new Test ();
Print_r ($ t-> getConstArray ());
?>
In the above code, the getConstArray () function is equivalent to an array constant.
In PHP, we cannot use const to directly define an array constant, but const can define a string constant, combined with the eval () function...