Home >Backend Development >PHP Tutorial >如果把变量作为常量的名称?

如果把变量作为常量的名称?

WBOY
WBOYOriginal
2016-06-06 20:47:341352browse

<code class="lang-php">define('DBNAME','test');
$val = 'DBNAME';
echo eval($val);
</code>

回复内容:

<code class="lang-php">define('DBNAME','test');
$val = 'DBNAME';
echo eval($val);
</code>

火速解答

eval是安全性最差的实践,几乎没有之一。请永远不要使用eval

使用字符串作为名称请求常量的值,请直接用constant函数。

<code class="lang-php">define("MAXSIZE", 100);
echo MAXSIZE;
echo constant("MAXSIZE"); // same thing as the previous line
</code>
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