我不知道我是否正确使用它
这是错误:
解析错误:语法错误,第 10 行/Users/*/test 中的意外"[",预期 '",".php 或 ';'
这是我的PHP代码:
$q[0] = "ahmad";
$q[1] = "mohammed";
$q[2] = "khaled";
function content(){
global $q[2];
$s =$q[2];
}
content();
echo $s;
不能对单个数组值使用 global
,只能对整个变量使用:
function content(){
global $q;
$s = $q[2];
}
请不要使用global。将其作为参数通过函数传递。使用global
通常是不好的做法。
$q[0] = "ahmad";
$q[1] = "mohammed";
$q[2] = "khaled";
function content($param){
return $param[2];
}
echo content($q);
你应该只$q
全局的,你仍然可以把它作为一个数组来访问。
只应从全局范围内获取$g。
function content(){
global $q;
$s =$q[2];
}
只需使用 $q
& $s
的global
。像这样的东西
<?php
$q[0] = "ahmad";
$q[1] = "mohammed";
$q[2] = "khaled";
function content(){
global $q,$s;
$s =$q[2];
}
content();
echo $s;
?>
修复了它!原因是您不能将数组中的特定键设置为全局键,还必须$s设置为全局键。
$q[0] = "ahmad";
$q[1] = "mohammed";
$q[2] = "khaled";
function content() {
global $q;
global $s;
$s = $q[2];
}
content();
echo $s;