我正在为样式指南生成器调整一些PHP来拉入.scss文件并显示它,但在第56行我得到一个通知:未定义变量。
这一行是
$sass = 'sass'.strstr($path, "/");
和整个函数。php是
<?php
// Display title of each markup samples as a list item
function listMarkupAsListItems ($type) {
$files = array();
$handle=opendir('markup/'.$type);
while (false !== ($file = readdir($handle))):
if(stristr($file,'.html')):
$files[] = $file;
endif;
endwhile;
sort($files);
foreach ($files as $file):
$filename = preg_replace("/'.html$/i", "", $file);
$title = preg_replace("/'-/i", " ", $filename);
$title = ucwords($title);
echo '<li><a href="#sg-'.$filename.'">'.$title.'</a></li>';
endforeach;
}
// Display markup view and source
function showMarkup($type) {
$files = array();
$handle=opendir('markup/'.$type);
while (false !== ($file = readdir($handle))):
if(stristr($file,'.html')):
$files[] = $file;
endif;
endwhile;
sort($files);
foreach ($files as $file):
$filename = preg_replace("/'.html$/i", "", $file);
$title = preg_replace("/'-/i", " ", $filename);
$documentation = 'doc/'.$type.'/'.$file;
echo '<div class="sg-markup sg-section">';
echo '<div class="sg-display">';
echo '<h2 id="sg-'.$filename.'" class="sg-h2">'.$title.'</h2>';
if (file_exists($documentation)) {
echo '<div class="sg-doc">';
include($documentation);
echo '</div>';
}
include('markup/'.$type.'/'.$file);
echo '</div>';
echo '<div class="sg-markup-controls"><button type="button" class="sg-btn sg-btn--source">View Source</button> <a class="sg-btn--top" href="#top">Back to Top</a></div>';
echo '<div class="sg-source sg-animated">';
echo '<button type="button" class="sg-btn sg-btn--select">Copy Source</button>';
echo '<pre class="line-numbers"><code class="language-markup">';
echo htmlspecialchars(file_get_contents('markup/'.$type.'/'.$file));
echo '</code></pre>';
echo '</div>';
echo '</div>';
$sass = 'sass'.strstr($path, "/");
$sass = str_replace(".html",".scss",$sass);
if (file_exists($sass)) {
echo '<div class="sg-sub-section">';
echo '<div class="sg-markup-controls">';
echo '<button type="button" class="sg-btn sg-btn--source">View Sass</button>';
echo '</div>';
echo '<div class="sg-source">';
echo '<button type="button" class="sg-btn sg-btn--select">Copy Sass</button>';
echo '<pre class="line-numbers"><code class="language-scss">';
echo file_get_contents($sass);
echo '</code></pre>';
echo '</div>';
echo '</div>';
}
endforeach;
}
?>
我肯定这是一些简单的PHP问题,我错了。如有任何帮助,我将不胜感激。
更新:$sass的代码取自另一个代码片段,看看两个注释(谢谢),似乎$path变量是问题所在?我想我需要将它定义为我要查找的文件夹路径?
您需要首先定义$path。您可以在其他地方执行此操作,并在函数开头的一行中调用$path变量,例如:
function showMarkup($type) {
global $path;
// ... etc.
或者在定义函数时为它定义一个默认值:
function showMarkup($type, $path="my-default-path/") {