php-只有一个生成的代码显示错误

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

我有一个代码列出我的所有目录没有扩展名,它运作良好。事情就是当我想在之前和之后添加一些php <>时,在<?php ?>中转换我的<!-- -->。我不知道为什么。有什么想法吗?

   <?php
    $variables = '';
    $handle = '';
    $variablesfile = '';
    // open my directory
    if ($handle = opendir($_SERVER['DOCUMENT_ROOT'].'/mvi/index/graphic-documents/sozie/variables/')) {
        // list directory contents
        while (false !== ($variablesfile = readdir($handle))) {
            // exeptions
            if (($variablesfile != ".")
            && ($variablesfile != "..")
            && ($variablesfile != "index.php")
            && ($variablesfile != "compiler.php")
            && ($variablesfile != ".DS_Store"))

            // only grab file names
            if (is_file($_SERVER['DOCUMENT_ROOT'].'/my/path/'. $variablesfile)) {
                $file_name = $variablesfile;
                $file_array = explode('.',$file_name);
                $extension = count($file_array) - 2;
                $no_extension = substr($file_name,0,strlen($file_array[$extension]));

                // creation of php code 
                $variables .= '<?php $'.$no_extension.' = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/'.$no_extension.'.txt")  ; ?>'. "\n" ; 
            }
        }

        closedir($handle);

        echo $variables ;

    }
    ?>

检查员的结果:

应该是什么

<?php $variable = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/variable.txt")  ; ?>

<!-- ?php $variable = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/variable.txt")  ; ? -->

如果我从Chrome查看我的源代码,<?php ?>显示良好.....

一切都在其他地方工作正常..我检查了我的apache,我在localhost运行..所以一切都很好。

php html variables comments
1个回答
1
投票

请记住,只有一次拍摄和一次拍摄,以便在处理此请求时呈现您需要做的任何事情。因此,您必须立即执行呈现响应所需的任何代码。

尝试通过定义关联数组来捕获结果:

$results = array();

然后使用$no_extension变量作为键将数据插入到该结构中:

$results[$no_extension] = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/my/path/$no_extension.txt");

所以最后你可以用这些数据做任何你想做的事情。

不要忘记PHP可以并且将在双引号字符串中插入$no_extension等变量。如果您尝试基于其他字符串组合字符串,这可以简化您的代码,避免重复连接。

© www.soinside.com 2019 - 2024. All rights reserved.