简体   繁体   English

PHP-回声要求在变量中传递代码

[英]php - echo require code passed in variable

I have some sudo code here 我这里有一些sudo代码

    $image_position = left

    switch($image_position){
        case 'left':
            $block_con = "require(locate_template('templates/block-media-left.php'))";
            break;
        case 'right':
            $block_con = 'require(locate_template("templates/block-media-right.php"))';
            break;
        case 'center':
            $block_con = 'require(locate_template("templates/block-media-center.php"))';
            break;  
        default:
            $block_con = 'require(locate_template("templates/block-text.php"))';

    }


    <div>

        <?php echo $block_con; ?>

    </div>

Depending on the value of the $image_position I want to load a different block of php found in a templates folder. 根据$ image_position的值,我想加载在templates文件夹中找到的另一块php。

The switch works but echo a string and does not add the require code. 该开关有效,但回显字符串,并且不添加必需代码。

I didn't think it would but don't know how to do this. 我不认为会,但是不知道该怎么做。

How about this? 这个怎么样?

<?php 
$image_position = 'left';
switch($image_position){
    case 'left':
        $block_con = 'templates/block-media-left.php';
        break;
    case 'right':
        $block_con = 'templates/block-media-right.php';
        break;
    case 'center':
        $block_con = 'templates/block-media-center.php';
        break;  
    default:
        $block_con = 'templates/block-text.php';
}
?>


<div>
    <?php require(locate_template($block_con)); ?>
</div>

It's important to note that this is just REQUIRING the PHP file currently. 重要的是要注意,这只是当前所需的PHP文件。 This means it'll be included and executed. 这意味着它将被包含并执行。 You'd have to echo in that included PHP file to get any output. 您必须在包含的PHP文件中回显任何输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM