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>"; }
我究竟做错了什么?为什么$ path在createList函数外部正确打印,但是在函数内部无法访问?
createList
有几种方法可以解决此问题:
1)使用亚历克斯所说的话,告诉函数它是一个全局变量:
echo $path; // working function createList($retval) { global $path; echo $path; // working
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
3)如果特定于该函数,则将其传递给该函数:
echo $path; // working function createList($retval, $path) { echo $path; // working
根据功能的实际工作原理,其中之一会起作用。