为什么PHP函数fopen()和相关变量在smarty模板引擎中的行为不同?

为什么PHP函数fopen()和相关变量在smarty模板引擎中的行为不同?,php,smarty3,Php,Smarty3,考虑一下代码片段: {if $showfile eq 'debug'} <pre>{php} if ( $fh = fopen('../logs/debug.log' , "r") ) { readfile('../logs/debug.log'); fclose($fh); } else { echo "Error: debug file

考虑一下代码片段:

{if $showfile eq 'debug'}
    <pre>{php}
        if ( $fh = fopen('../logs/debug.log' , "r") ) {
            readfile('../logs/debug.log'); 
            fclose($fh);
        } else {
            echo "Error: debug file can not be read.";
        }
    {/php}</pre>
{/if}
Error:  file can not be read.
{/if} 显示以下输出:


有什么问题?为什么
$showfile
变量在
{if$showfile eq'debug'}
条件之后变为空…

Smarty中的
{php}
标记有自己的作用域,就像php函数一样。如果要使用
$showfile
变量,则需要使用全局关键字

{if $showfile eq 'debug'}
    <pre>{php}
        if ( $fh = fopen('../logs/'.$showfile.'.log' , "r") ) {
            readfile('../logs/'.$showfile.'.log'); 
            fclose($fh);
        } else {
            echo "Error: ".$showfile." file can not be read.";
        }
    {/php}</pre>
{/if}
{php}
    global $showfile;
{/php}