Home > php教程 > php手册 > body text

浅谈PHP eval()函数定义和用法,浅谈eval

WBOY
Release: 2016-07-06 14:24:48
Original
1061 people have browsed it

浅谈PHP eval()函数定义和用法,浅谈eval

eval() 函数把字符串按照 PHP 代码来计算。

该字符串必须是合法的 PHP 代码,且必须以分号结尾。

如果没有在代码字符串中调用 return 语句,则返回 NULL。如果代码中存在解析错误,则 eval() 函数返回 false。

语法

eval(phpcode) 
Copy after login

参数 描述
phpcode 必需。规定要计算的 PHP 代码。

提示和注释

注释:返回语句会立即终止对字符串的计算。

注释:该函数对于在数据库文本字段中供日后计算而进行的代码存储很有用。

例子

<&#63;php
$string = "beautiful";
$time = "winter";
 
$str = 'This is a $string $time morning!';
echo $str. "<br />";
 
eval("\$str = \"$str\";");
echo $str;
&#63;>    
Copy after login

输出:

This is a $string $time morning!
This is a beautiful winter morning!

eval() 函数在CodeIgniter框架里也有用到。在 /system/database/DB.php 文件中,根据系统的配置动态的定义了一个类 CI_DB,具体代码片段如下:

if ( ! isset($active_record) OR $active_record == TRUE)
 {
 require_once(BASEPATH.'database/DB_active_rec.php');
 
 if ( ! class_exists('CI_DB'))
 {
  eval('class CI_DB extends CI_DB_active_record { }');
 }
 }
 else
 {
 if ( ! class_exists('CI_DB'))
 {
  eval('class CI_DB extends CI_DB_driver { }');
 }
 }
 
 require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
 
 // Instantiate the DB adapter
 $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
 $DB = new $driver($params);
Copy after login

以上这篇浅谈PHP eval()函数定义和用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持帮客之家。

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!