Home  >  Article  >  Backend Development  >  How to define array constants (array constants) in PHP, array constant_PHP tutorial

How to define array constants (array constants) in PHP, array constant_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:54931browse

How to define array constants (array constants) in PHP, array constants

In PHP, we cannot use const to directly define array constants, but const can define string constants, and combined with the eval() function, the string constants can be executed. Therefore, we can return an array constant by defining a string constant. Here is the moment for us to witness the miracle!

Copy code The code is as follows:

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 PHP code
}
}
$t=new Test();
print_r($t->getConstArray());
?>

In the above code, the getConstArray() function is equivalent to an array constant.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/912672.htmlTechArticleHow to define array constants (array constants) in PHP. Array constants are in PHP and we cannot use const to define them directly. Array constants, but const can define string constants, combined with the eval() function...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn