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

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

WBOY
Release: 2016-06-06 20:47:34
Original
1328 people have browsed it

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

回复内容:

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

火速解答

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

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

<code class="lang-php">define("MAXSIZE", 100);
echo MAXSIZE;
echo constant("MAXSIZE"); // same thing as the previous line
</code>
Copy after login
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template