PHP eval() 函数

PHP 教程 · 2019-01-27 12:44:20

实例

把字符串当成 PHP 代码来计算:

<?php $string = "beautiful"; $time = "winter"; $str = 'This is a $string $time morning!'; echo $str. PHP_EOL; eval("\$str = \"$str\";"); echo $str; ?>

以上代码执行输出结果为:

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

定义和用法

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

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

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

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

语法

eval(phpcode)

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

技术细节

返回值: 除非在代码字符串中调用 return 语句,则返回传给 return 语句的值,否则返回 NULL。如果代码字符串中存在解析错误,则 eval() 函数返回 FALSE。
PHP 版本: 4+

点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

The  C Programming Language

The C Programming Language

Brian W. Kernighan、Dennis M. Ritchie / Prentice Hall / 1988-4-1 / USD 67.00

Presents a complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the finalized ANSI standard for C while showing how to ta......一起来看看 《The C Programming Language》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

SHA 加密
SHA 加密

SHA 加密工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换