wai*_*933 0 php user-defined-functions
我遇到以下代码的问题.它应该做的是回显cats.php,然后是example.php,但它并没有回应example.php.任何想法为什么会这样?
$bookLocations = array(
'example.php',
'cats.php',
'dogs.php',
'fires.php',
'monkeys.php',
'birds.php',
);
echo $bookLocations[1];
function findfile($filenumber)
{
echo $bookLocations["$filenumber"];
}
findfile(0);
Run Code Online (Sandbox Code Playgroud)
尝试改变,
echo $bookLocations["$filenumber"];
Run Code Online (Sandbox Code Playgroud)
至:
echo $bookLocations[$filenumber];
Run Code Online (Sandbox Code Playgroud)
编辑*要扩展Thomas的正确答案,而不是使用全局变量,您可以将方法更改为:
function findfile($filenumber, $bookLocations)
{
echo $bookLocations[$filenumber];
}
Run Code Online (Sandbox Code Playgroud)
我相信你可能还需要在你的函数中声明全局变量.
global $bookLocations;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
213 次 |
| 最近记录: |