如何从eval函数打印值

问题描述 投票:0回答:1

我有一个表'test_config'。我有一个字段'config_name','config_value'。

config_name |  config_value
____________________________ 
    test    |    "if($test=='1'){echo 'hello executive if part';}
            |    else{echo 'executive else part';}"  

在我的php页面中,我将代码作为

<?php
        $test='1';
        $bottom=DB::table('test_config')->where('config_name','test')->first()->config_value;
        eval("\$bottom=\"$bottom\";");
        print_r($bottom);
        ?>

我只打印'hello executive if if part'。我在eval()中找到了错误'FatalErrorException'代码行1:语法错误,如果(T_IF)'则意外

php eval
1个回答
0
投票

以下代码对我有用:

<?php
$test='1';
$bottom="if($test=='1'){echo 'hello executive if part';} else{ echo 'executive else part';} ";
eval($bottom);
//print_r($bottom);
?>

或者像这样使用:

<?php
$test='1';
$bottom="if($test=='1'){echo 'hello executive if part';} else{ echo 'executive else part';}";
eval("\$bottom=\"$bottom\";");
eval($bottom);
?>
© www.soinside.com 2019 - 2024. All rights reserved.