echo $path; //working
function createList($retval) {
echo $path; //not working
print "<form method='POST' action='' enctype='multipart/form-data'>";
foreach ($retval as $value) {
print "<input type='checkbox' name='deletefiles[]' id='$value' value='$value'>$value<br>";
}
print "<input class='submit' name='deleteBtn' type='submit' value='Datei(en) löschen'>";
print "</form>";
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?为什么$ path在createList函数外部正确打印,但在函数内部无法访问?
Jos*_*ter 36
有几种方法可以解决这个问题:
1)通过告诉函数它是一个全局变量来使用Alex所说的:
echo $path; // working
function createList($retval) {
global $path;
echo $path; // working
Run Code Online (Sandbox Code Playgroud)
2)将其定义为常量:
define(PATH, "/my/test/path"); // You can put this in an include file as well.
echo PATH; // working
function createList($retval) {
echo PATH; // working
Run Code Online (Sandbox Code Playgroud)
3)如果它特定于该功能,则将其传递给函数:
echo $path; // working
function createList($retval, $path) {
echo $path; // working
Run Code Online (Sandbox Code Playgroud)
根据功能如何真正起作用,其中一个会做你的.