有什么区别? eval() 还是直接调用函数?

发布于 2024-08-27 17:45:14 字数 420 浏览 3 评论 0原文

我不是 php 专家,我不知道 a 和 b 之间有什么区别(1)。

a.)eval('return "'.base64_decode("encoded_text").'";')

b.)base64_decode("encoded_text")

-我认为,a是 php 代码,b 只是字符串。我的另一个问题是:

c 和 d 之间有什么区别(2)?

c.)eval('return "'.base64_decode("encoded_text").'";')

d.)eval(base64_decode("encoded_text"))

所以我有 2 个问题。谁可以回答/帮助?

谢谢。

I'm not php expert and I don't know what's the difference(1) between a and b.

a.)eval('return "'.base64_decode("encoded_text").'";')

b.)base64_decode("encoded_text")

-I THINK, a is php code and b is just string. And my other question is:

What is the difference(2) between c and d?

c.)eval('return "'.base64_decode("encoded_text").'";')

d.)eval(base64_decode("encoded_text"))

So I have 2 questions. Who can answer/help ?

Thanks.

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

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

发布评论

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

评论(3

相权↑美人 2024-09-03 17:45:14

让我们将您的 2 个案例标记为案例 X(a 和 b 部分)和 Y(c 和 d 部分)。

案例 X

对于这一点,两个部分彼此没有区别。事实上,a部分有一些冗余。

如果您慢慢评估它们,您会发现它是多么多余:

a 部分
在这一部分中,不同之处在于您在字符串中添加带有 returneval 语句进行评估。

  1. echo eval('return "'.base64_decode("encoded_text").'";');
  2. echo eval('return "decoded_text";')'
  3. echo "decoded_text";

b 部分

  1. echo base64_decode("encoded_text");
  2. echo "decoded_text";

案例 Y

对于此,存在着严重的差异。

c 部分

  1. echo eval('return "'.base64_decode("encoded_text").'";');
  2. echo eval('return "decoded_text";' )'
  3. echo "decoded_text";

d 部分

  1. echo eval(base64_decode("encoded_text"));
  2. echo eval ("decoded_text"); - 这里可能存在语法错误,因为 decoded_text 可能是也可能不是正确的 PHP 代码。

Let's label your 2 cases as Case X (part a and b) and Y (part c and d).

Case X

For this, both of the parts have no difference from each other. In fact, part a has some redundancy.

If you evaluate them slowly, you will notice how redundant it is:

Part a
In this part, the difference is that you add the eval statement with return in the string for evaluation.

  1. echo eval('return "'.base64_decode("encoded_text").'";');
  2. echo eval('return "decoded_text";')'
  3. echo "decoded_text";

Part b

  1. echo base64_decode("encoded_text");
  2. echo "decoded_text";

Case Y

For this, there's grave difference.

Part c

  1. echo eval('return "'.base64_decode("encoded_text").'";');
  2. echo eval('return "decoded_text";')'
  3. echo "decoded_text";

Part d

  1. echo eval(base64_decode("encoded_text"));
  2. echo eval("decoded_text"); - there may be a syntax error here, because decoded_text may or may not be proper PHP code.
懷念過去 2024-09-03 17:45:14

编辑:哎呀,第二个问题读错了。

对于第一个问题:在一种情况下,eval() 无缘无故地被使用。仅当您出于某种原因将某些 PHP 代码动态构建到字符串中时才需要 eval(),并且只能非常非常小心地使用。当然没有必要作为直接调用函数的替代方案。

至于第二个问题,区别在于正在评估哪个字符串。情况(c) 将返回base-64 解码“编码文本”的结果。也就是说,它将返回一个解码版本。情况 (d) 将首先解码文本,然后尝试将其作为 PHP 代码执行。所以(d)实际上执行了解码的结果,(c)则没有,它只是返回解码后的文本。

Edit: whoops, read second question incorrectly.

For the first question: In one case eval() is being used for no reason. eval() is only necessary if you are dynamically building some PHP code into a string for some reason, and should only be used very, very carefully. It's certainly not necessary as an alternative to just calling the function directly.

As for the second question, the difference is which string is being evaluated. Case (c) will return the result of base-64 decoding "encoded text". That is, it'll return a decoded version. Case (d) will first decode the text, then try to execute it as PHP code. So (d) actually executes the result of decoding, (c) does not, it just returns the decoded text.

青柠芒果 2024-09-03 17:45:14

忘记eval,现阶段尽量不要使用。
之间有什么区别

$var = base64_decode("encoded_text")

你应该问和

return base64_decode("encoded_text");

正如乍得所写,尽量避免 eval!它只执行$variable中的代码。例子,

$var = 'base64_decode("encoded_text")';
return eval($var);

Forget about eval, at this stage try not to use.
You should ask what's difference between

$var = base64_decode("encoded_text")

and

return base64_decode("encoded_text");

As Chad wrote, try to avoid eval! It only executes code in $variable. example,

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