Home > Backend Development > PHP Tutorial > 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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:13:54
Original
1009 people have browsed it

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...
Related labels:
php
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template