为什么这在 PHP 中不起作用?

发布于 2024-08-22 18:30:02 字数 433 浏览 2 评论 0原文

$constPrefix = '_CONST_';

if (strstr($content, $constPrefix)) {
    $constants = array('PHP_VERSION', '__FILE__');
    foreach($constants as $constant) {
        $constantOutput = eval($constant);
        $content = str_replace($constPrefix . $constant, $constantOutput, $content);
    }
}

基本上,只是尝试解析一些内容并用等效的 PHP 常量替换其中的字符串。我应该在这里使用 eval() 吗?我以前从未真正找到过使用它的理由,而且现在已经快凌晨 1 点了,我想知道这是否是巧合?

$constPrefix = '_CONST_';

if (strstr($content, $constPrefix)) {
    $constants = array('PHP_VERSION', '__FILE__');
    foreach($constants as $constant) {
        $constantOutput = eval($constant);
        $content = str_replace($constPrefix . $constant, $constantOutput, $content);
    }
}

Basically, just trying to parse some content and replace strings inside with the equivalent PHP constant. Is eval() what I should be using here? I've never actually found a reason to use it before, and it's almost 1am, and am wondering if that is a coincidence?

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

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

发布评论

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

评论(2

我是有多爱你 2024-08-29 18:30:02

您可以将 eval 替换为 constant

$constantOutput = constant($constant);

You can replace eval with constant:

$constantOutput = constant($constant);
断桥再见 2024-08-29 18:30:02

你为什么不忽略 eval 呢?

<?php
    $v = PHP_VERSION;
    $f = __FILE__;

    echo $v.' '.$f;
?>

给出

/tmp% php test.php 
5.2.10-2ubuntu6.4 /tmp/test.php

Why don't you just leave out the eval?

<?php
    $v = PHP_VERSION;
    $f = __FILE__;

    echo $v.' '.$f;
?>

gives

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