search
Homephp教程php手册PHP函数eval()介绍和使用示例,eval示例

PHP函数eval()介绍和使用示例,eval示例

什么是eval()?

eval() 函数把字符串按照 PHP 代码来计算。
该字符串必须是合法的 PHP 代码,且必须以分号结尾。
如果没有在代码字符串中调用 return 语句,则返回 NULL。如果代码中存在解析错误,则 eval() 函数返回 false。

语法

eval(phpcode)

复制代码 代码如下:


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

例子1

复制代码 代码如下:


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

输出:
This is a $string $time morning!
This is a beautiful winter morning!

例子2

我们使用for循环创建n个随便,值成倍增加

复制代码 代码如下:


 
for($i=1;$i         eval('$a'.$i.'='.($i*$i).';');
}
 
for($i=1;$i         eval('echo $a'.$i.'.\'
\' ;');
}
 
echo '
';
echo $a1 + $a10;


输出:

复制代码 代码如下:


1
4
9
16
25
36
49
64
81
100
101

PHP中eval()函数的使用方法

eval() 括号中必须是字符串

假如你写 $a=1; eval("echo (\$a+\$b);"); 运行结果是 1

如果是 $a=1; eval("echo ($a+$b);"); 则报错

这说明里面最好是严格的字符串 并且是完整的PHP语句

$a=1; eval("echo \$a;");
$a=1; eval("echo $a;");
上面结果一样 以为第一个eval 是执行了 echo $a; 而第二句是执行 echo 1;
 

php怎使用eval()函数更改这个实例?

$f ='$';

$f.$a.$this->i
这是什么写法,显然应该就不是你要的效果,这相当于字符串连接了。大概你的意思是要这样的,$($a.$this->i)。 其他的再慢慢考虑吧
 

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 Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function