Using global vars within a function in PHP the way you do it in Javascript

后端 未结 5 1853
渐次进展
渐次进展 2021-01-27 15:22

I have a function that uses lots of global vars and arrays - e.g.

$a=1;
$b[0]=\'a\';
$b[1]=\'b\';
$c=\'Hello\';

function foo() {
 echo \"$a 
       $b[0] 
              


        
5条回答
  •  走了就别回头了
    2021-01-27 15:42

    When ever you have a function, the context management in PHP 5.3 doesn't allow access to global variables without the use of the "global" keyword.

    Php.net has a post about the topic here.

    function foo() {
        global $a, $b, $c;
        echo "$a 
        $b[0] 
        $b[1] 
        $c";
    }
    

提交回复
热议问题