不用return怎么改变改变全局变量的值?
<?php
$hello="hello world";
function test($x){
$GLOBALS[$x]=$x." good boy";
}
test($hello);
echo $hello; //结果是“hello world”
//上面的代码为什么不能直接改变$hello的值?能否不用下面这种方法?
echo "<br>";
function test1($x){
$x=$x." good boy";
return $x;
}
$hello=test1($hello);
echo $hello; //结果是“hello world good boy”
?>
<?php
$hello="hello world";
function test($x){
$GLOBALS[$x]=$x." good boy";
}
test($hello);
echo $hello; //结果是“hello world”
//上面的代码为什么不能直接改变$hello的值?能否不用下面这种方法?
echo "<br>";
function test1($x){
$x=$x." good boy";
return $x;
}
$hello=test1($hello);
echo $hello; //结果是“hello world good boy”
?>
$hello="hello world";
function test($x){
$GLOBALS['hello']=$x." good boy";
}
test($hello);
echo $hello; //结果是“hello world”
如我的变量时 $world="world"; 通过test($world) 出来的结果是 "world good boy"
global $hello;
$hello = "hello world";
function test($x){
$GLOBALS[$x]=$x." good boy";
}
test("hello"); //在此处 不能用 $hello 要用 "hello" ,就是你 变量 的名字
echo $hello; //结果是“hello world”
//上面的代码为什么不能直接改变$hello的值?能否不用下面这种方法?
echo "<br>";
function test1($x){
$x=$x." good boy";
return $x;
}
$hello=test1($hello);
echo $hello; //结果是“hello world good boy”
?>
$world="world"; ~~~~~ 一样 可以 解决 问题 test("world") ;
function test($name,$value){
//
}
test('hello',$hello);
$hello="hello world";
function test(&$x){
$x=$x." good boy";
}
test($hello);
echo $hello; //结果是“hello world”
?>
function test22($x){
$GLOBALS[$x].=" good boy";
}
test22('hello');
echo $hello; //结果是“hello world”