在php中使用包含()的变量

在php中使用包含()的变量,php,web,Php,Web,此代码输出路径 function genPost() { // Gives the full path leading to this file starting at root. // eg. /var/www/html $path = dirname(__FILE__); // Lists the folders and files of the chosen path. // FYI the variable is now an array!!! $pathContents = scan

此代码输出路径

function genPost() {
// Gives the full path leading to this file starting at root.
// eg. /var/www/html
$path = dirname(__FILE__);

// Lists the folders and files of the chosen path.
// FYI the variable is now an array!!!
$pathContents = scandir($path);

function makePath($key) {
    $path = dirname(__FILE__);
    $folders = scandir($path);
    $two = $folders[$key];
    $three = $path . "/" . $two;
    echo $three . "<br>";
    //echo include("$three");
}
$key = 0;
// array_key_exists() returns false when the key to the array doesn't exist
while (array_key_exists($key, $pathContents)) {
    makePath($key);
    $key = $key + 1;
}
}
echo genPost();

我的问题是如何避免将目录放在include中,而是使用变量告诉php解释器从何处提取include函数的文件。

使用include with string(通常应该是表示路径的字符串,而不是函数调用)会导致错误的路径错误

您的makePath应该是:

function makePath($key) {
    $path = dirname(__FILE__);
    $folders = scandir($path);
    $two = $folders[$key];
    $three = $path . "/" . $two;
    return $three;
}
当您需要使用它时:

while (array_key_exists($key, $pathContents)) {
   include makePath($key);
   $key = $key + 1;
}

我正在尝试制作一个脚本,用于在博客上显示帖子。这是一个如何动态操作Include()函数的实验的开始。我发现你的问题还不清楚。我无法复制您试图描述的内容,因为我看不到任何地方声明或调用了
getSum()。你是不是想
echo include
?我更新了帖子。对不起,我不清楚。让我知道this help.PHP是否解析双引号字符串中的变量,但不解析函数调用<代码>包括“makePath($key)”需要
包含makePath($key)该函数需要返回路径,而不是回显路径。回显包含不起作用。哦,天哪!就是这样!!我没注意到echo$3的打字错误。“
”;谢谢你对退货的建议,以及!!!
while (array_key_exists($key, $pathContents)) {
   include makePath($key);
   $key = $key + 1;
}