在html上显示一个带有html和php函数调用的php块


Displaying a block of php with html and php function call on the html

all。

我正在处理一个if语句,其中我使用php来检查是否设置了会话语言。如果设置了变量,我将显示一些html,其中html标记上嵌入了它自己的php代码。我已经转义了字符,但仍然没有得到要在html上显示的翻译函数调用响应。如果您能为我提供以下代码方面的任何帮助,我将不胜感激。提前谢谢。

<?php
if((isset($_SESSION['lang'])) && $_SESSION['lang']=='pt'){
echo("<div class='"row'">
    <div class='"col col-12'" style='"width:100'% !important'">
    <label class='"radio state-success'"><input type='"radio'" name='"chosenMethod'" value='"2'"><i style='"padding-right:0px !important;'">    
    </i><?php echo getTranslation(''member'');?></label>
    </div><br>
    <div class='"col col-12'" style='"width:100'% !important'">
    <div style='"font-size:90'%'">echo getTranslation(''member_choices'');</div>
    </div><br>
    </div>");}
    ?>

替换此行

</i><?php echo getTranslation(''member'');?></label>

带有

</i>" . getTranslation('member') . "</label>

由于您已经在回显语句,;?>部分被解释为原始php语句的结束标记。

通过使用echo,您告诉PHP不要执行以下HTML/PHP组合,而是打印。因此,您想要的是

$member=getTranslation('member');
$memberChoices=getTranslation(''member_choices'');
echo "<div class='"row'">
    <div class='"col col-12'" style='"width:100'% !important'">
    <label class='"radio state-success'"><input type='"radio'" name='"chosenMethod'" value='"2'"><i style='"padding-right:0px !important;'">    
    </i>$result</label>
    </div><br>
    <div class='"col col-12'" style='"width:100'% !important'">
    <div style='"font-size:90'%'">$memberChoices</div>
    </div><br>
    </div>";

让我补充一点,这不是最优雅的方式!

如下修复(您在<?php中使用<?php,在echo中使用echo):

<?php
if((isset($_SESSION['lang'])) && $_SESSION['lang']=='pt'){
    $translation = getTranslation(member);
    $translation_choice = getTranslation(member_choices);
    echo("<div class='"row'">
        <div class='"col col-12'" style='"width:100'% !important'">
        <label class='"radio state-success'"><input type='"radio'" name='"chosenMethod'" value='"2'"><i style='"padding-right:0px !important;'">    
        </i>" . $translation . "</label>
        </div><br>
        <div class='"col col-12'" style='"width:100'% !important'">
        <div style='"font-size:90'%'">" . $translation_choice . "</div>
        </div><br>
        </div>");}
    ?>