具有不同变量名的相同代码在PHP中不起作用?

时间:2015-02-13 14:46:30

标签: php glob

我正在使用此代码列出目录中完美运行的所有文件

<?php 
$exclude = array("index.php","cssheadertop.php","cssheaderbottom.php");
$cssfiles = array_diff(glob("*.php"), $exclude); 
foreach ($cssfiles as $cssfile) {
$filename = "http://example.com/lessons/css/".$cssfiles[$cssfile];
outputtags($filename,true,true); 
}
?>

但是,使用此代码,网页上不会显示任何内容。我无法弄明白为什么

<?php 
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude); 
foreach ($htmlfiles as $htmlfile) {
$filename = "http://example.com/lessons/html/".$htmlfiles[$htmlfile];
outputtags($filename,true,true); 
}
?>

2 个答案:

答案 0 :(得分:1)

试试这个:

<?php
$exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
$htmlfiles = array_diff(glob("*.php"), $exclude); 
foreach ($htmlfiles as $htmlfile) {
    $filename = "http://example.com/lessons/html/".$htmlfile;
    outputtags($filename,true,true); 
}
?>

$ htmlfiles [$ htmlfile]不应该设置,也不应该有用。

答案 1 :(得分:0)

您需要在foreach循环中使用$htmlfile而不是$htmlfiles[$htmlfile],并且它适用于任何其他变量的名称

<?php 
   $exclude = array("index.php","htmlheadertop.php","htmlheaderbottom.php");
   $htmlfiles = array_diff(glob("*.php"), $exclude); 
   foreach ($htmlfiles as $htmlfile) {
     $filename = "http://example.com/lessons/html/".$htmlfile;
     outputtags($filename,true,true); 
   }
?>