Why does this not work in PHP?

$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, I'm just trying to parse some content and replace the lines inside with an equivalent PHP constant. Am i using here eval()? I never found a reason to use it before, and it's almost 1am, and I wonder if this is a coincidence?

+3
source share
2 answers

You can replace evalwith constant :

$constantOutput = constant($constant);
+10
source

Why don't you just leave eval?

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

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

gives

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

Source: https://habr.com/ru/post/1734251/


All Articles