search
Homephp教程php手册PHP中定义数组常量(array常量)的方法

这篇文章主要介绍了PHP中定义数组常量(array常量)的方法,本文在类中使用了const关键字和eval()函数来实现,需要的朋友可以参考下

在PHP中,我们不能用const直接定义数组常量,但是const可以定义字符串常量,结合eval()函数使字符串常量能执行。所以,我们可以用定义字符串常量的方式来返回一个数组常量。下面就是我们来见证奇迹的时刻!

复制代码 代码如下:


class Test
{
 const MY_ARR="return array(\"a\",\"b\",\"c\",\"d\");";
 public function getConstArray()
 {
  return eval(Test::MY_ARR);//eval()函数把字符串作为PHP代码执行
 }
}
$t=new Test();
print_r($t->getConstArray());
?>

上面代码中,getConstArray()函数,就相当于一个数组常量。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor