views:

105

answers:

2
$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?

A: 

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
Larry_Croft
Just wanted to be more flexible. Though I know `eval` *is* a performance hit.
alex
+7  A: 

You can replace eval with constant:

$constantOutput = constant($constant);
dusan