不用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”
?>

解决方案 »

  1.   

    <?php
        $hello="hello world";
        function test($x){
            $GLOBALS['hello']=$x." good boy";
        }
        test($hello);
        echo $hello;    //结果是“hello world”
      

  2.   

    我的意思是 $hello 作为本身是不定名字,
    如我的变量时 $world="world"; 通过test($world) 出来的结果是 "world good boy"
      

  3.   

    全局变量作为函数变量传入,随之全局变量自己的值也变化,而非只有GLOBALS['hello']
      

  4.   

    <?php
        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”
    ?>
      

  5.   

    在调用的时候  用 test("hello");  而 不是 test($hello);
    $world="world";  ~~~~~ 一样 可以 解决 问题     test("world")  ;
      

  6.   

    不行,你的结果是hello good boy 少了 world
      

  7.   

    没注意  ,要不 你 做 俩 参数吧  , 一个 键 name  值 value 
    function test($name,$value){
    //
    }
    test('hello',$hello);
      

  8.   

    看来还是用我原来的return方法好了,貌似没更好的方法
      

  9.   

    <?php
        $hello="hello world";
        function test(&$x){
            $x=$x." good boy";
        }
        test($hello);
        echo $hello;    //结果是“hello world”
    ?>
      

  10.   

    我给代码 结果是“hello world good boy”,忘记去你的注释了.
      

  11.   

    jlzan1314的好用,关于  &$var 有没有文章介绍一下,百度不知道该输入什么
      

  12.   

    $hello="hello world";
    function test22($x){
    $GLOBALS[$x].=" good boy";
    }
    test22('hello');
    echo $hello;    //结果是“hello world”
      

  13.   

      13 楼  BooJS   正解