【发布时间】:2018-06-01 08:35:06
【问题描述】:
我有一个代码可以列出我所有的目录,没有扩展名,它运行良好。问题是当我想在之前和之后添加一些 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对您的浏览器毫无意义。它被解释为评论,因为它没有其他意义。 -
yes... 在 Chrome 检查器中
-
您无法将 PHP 代码发送到您的浏览器。您必须在 PHP 环境中执行它。我不确定为什么你有 PHP 代码发出 PHP 代码。也许你能解释一下原因。
-
这里的关键是你不能只发出
<?php并让浏览器处理,因为它不能,Chrome 不知道 PHP 是什么,而且很可能永远不会。您需要返回 HTML,或者在您发送回 JSON 数据的情况下,无论如何都需要返回格式良好的内容。 PHP 代码不会削减它。
标签: php html variables comments