PHP eval $a="$a"?

发布于 2024-10-23 14:19:09 字数 174 浏览 2 评论 0 原文

我正在查看一些工作代码,并遇到了这一行:

eval("\$element = \"$element\";");

我真的很困惑为什么任何 PHP 开发人员都会编写这一行。除了为自身设置一个变量之外,这还有什么目的?

幸运的是,该行所在的函数从未被调用。

I was looking through some code for work, and came across this line:

eval("\$element = \"$element\";");

I'm really confused as to why any PHP developer would've written this line. What purpose does this serve, besides setting a variable to itself?

Luckily, the function this line is in is never called.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼中杀气 2024-10-30 14:19:09
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?> 

上面的示例将输出:

This is a $string with my $name in it.
This is a cup with my coffee in it.

http://php.net/manual/en/function.eval .php

我只是ctl+c ctrl+v :-)

<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?> 

The above example will output:

This is a $string with my $name in it.
This is a cup with my coffee in it.

http://php.net/manual/en/function.eval.php

I do just ctl+c ctrl+v :-)

爱你是孤单的心事 2024-10-30 14:19:09

它将变量的值转换为字符串,但我不建议使用它。

请改用函数 strval()。查看手册

It converts the value of the variable to a string, but I wouldn't recommend using it.

Use the function strval() instead. Have a look at the manual.

哀由 2024-10-30 14:19:09

这会将变量 $element 的字符串转换内容分配给名为 $element 的变量。另一种方法是使用 strval,或者在某些情况下使用 print_r($x, true)

This assigns the string-converted contents of the variable $element to a variable called $element. Another way to do this is to use strval, or in some cases print_r($x, true)

夏尔 2024-10-30 14:19:09

除了将值转换为字符串之外,它实际上并没有做太多事情,或者可能作为 sprintf 的一个糟糕替代品。但如果变量包含双引号,这会引起一些麻烦。您确实不想评估这样的代码:

$element = 'foo"bar';

更不用说一些更有害的代码了。似乎是“php 注入”的地方:D

不要使用它。

It doesn't really do much except converting the value to string or might serve as a poor alternative to sprintf. But if the variable contains double quotes, this is gonna cause some trouble. You really wouldn't want to eval a code like this:

$element = 'foo"bar';

Not to mention some even more harmful code. Seems like a place for a "php injection" :D

Don't use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文